Skip to content

Commit

Permalink
add deleteTrackedData
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Moschkin committed Sep 14, 2024
1 parent 4e398a0 commit f911b92
Show file tree
Hide file tree
Showing 4 changed files with 646 additions and 235 deletions.
60 changes: 53 additions & 7 deletions app/abstract/voyagetracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,56 @@ export interface TrackerPostResult {

export abstract class VoyageTrackerBase {

async deleteTrackedVoyage(
dbid?: number,
trackerId?: number
): Promise<ApiResult> {
Logger.info("Tracked Voyage data", { dbid, trackerId });

if (!dbid || !trackerId)
return {
Status: 400,
Body: { result: "bad input" },
};

const timeStamp = new Date();
let res: boolean;

try {
res = await this.deleteVoyageByTrackerId(dbid, trackerId);
if (!res) {
return {
Status: 400,
Body: {
dbid: dbid,
error: "Unable to delete record."
},
};
}
} catch (err) {
if (typeof err === "string") {
return {
Status: 500,
Body: err,
};
} else if (err instanceof Error) {
return {
Status: 500,
Body: err.toString(),
};
}
}

return {
Status: 200,
Body: {
dbid: dbid,
trackerId: trackerId
},
};

}

async postTrackedVoyage(
dbid: number,
voyage: ITrackedVoyage,
Expand Down Expand Up @@ -224,13 +274,7 @@ export abstract class VoyageTrackerBase {
Logger.info("Get voyage data", { dbid, trackerId });
let assignments: TrackedCrew[] | null = null;

if (!dbid && !trackerId)
return {
Status: 400,
Body: { result: "bad input" },
};

if (!dbid)
if (!dbid || !trackerId)
return {
Status: 400,
Body: { result: "bad input" },
Expand Down Expand Up @@ -333,6 +377,8 @@ export abstract class VoyageTrackerBase {
}
}

protected abstract deleteVoyageByTrackerId(dbid: number, trackerId: number): Promise<boolean>;

protected abstract getVoyagesByDbid(dbid: number): Promise<TrackedVoyage[] | null>;

protected abstract getVoyagesByTrackerId(dbid: number, trackerId: number): Promise<TrackedVoyage[] | null>;
Expand Down
82 changes: 50 additions & 32 deletions app/controllers/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ router.post('/telemetry', async (req: Request, res: Response, next) => {
});


router.get('/voyagesByCrew', async (req: Request, res: Response, next) => {
try {
router.get('/voyagesByCrew', async (req: Request, res: Response, next) => {
try {
let cstr = req.query.crew?.toString();
let dstr = req.query.days?.toString();
let opAnd = (req.query.opand === '1');
let opAnd = (req.query.opand === '1');
let crew = cstr?.split(",");
let days = dstr ? Number.parseInt(dstr) : undefined;

Expand All @@ -227,7 +227,7 @@ router.get('/queryAlive', async (req: Request, res: Response, next) => {
try {
let apiResult = {
Status: 200,
Body: {
Body: {
service: req.query.what,
result: "UP"
}
Expand All @@ -244,12 +244,12 @@ router.post('/postProfile', async (req: Request, res: Response, next) => {
if (!req.body) {
res.status(400).send('Whaat?');
return;
}
}
try {
let playerData = req.body as PlayerData;
let apiResult = await DataCoreAPI.postPlayerData(playerData.player.dbid.toString(), playerData, getLogDataFromReq(req));
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
} catch (e) {
next(e);
}
});
Expand Down Expand Up @@ -285,13 +285,13 @@ router.post('/postVoyage', async (req: Request, res: Response, next) => {
if (!req.body) {
res.status(400).send('Whaat?');
return;
}
}
try {
let dbid = req.body.dbid;
let voyage = req.body.voyage as ITrackedVoyage;
let voyage = req.body.voyage as ITrackedVoyage;
let apiResult = await VoyageTrackerAPI.postTrackedVoyage(dbid, voyage, getLogDataFromReq(req));
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
} catch (e) {
next(e);
}
});
Expand All @@ -302,7 +302,7 @@ router.get('/getVoyages', async (req: Request, res: Response, next) => {
return;
}

try {
try {
let dbid = req.query?.dbid ? Number.parseInt(req.query.dbid.toString()) : undefined;
let trackerId = req.query?.trackerId ? Number.parseInt(req.query.trackerId.toString()) : undefined;
let apiResult = await VoyageTrackerAPI.getTrackedVoyages(dbid, trackerId);
Expand All @@ -316,15 +316,15 @@ router.post('/postAssignment', async (req: Request, res: Response, next) => {
if (!req.body) {
res.status(400).send('Whaat?');
return;
}
}

try {
let dbid = Number.parseInt(req.body.dbid);
let crew = req.body.crew;
let crew = req.body.crew;
let assignment = req.body.assignment as ITrackedAssignment;
let apiResult = await VoyageTrackerAPI.postTrackedAssignment(dbid, crew, assignment, getLogDataFromReq(req));
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
} catch (e) {
next(e);
}
});
Expand All @@ -333,8 +333,8 @@ router.post('/postAssignments', async (req: Request, res: Response, next) => {
if (!req.body) {
res.status(400).send('Whaat?');
return;
}
}

try {
let dbid = Number.parseInt(req.body.dbid);
let assign = req.body.assignments as { [key: string]: ITrackedAssignment[] };
Expand All @@ -349,13 +349,13 @@ router.post('/postAssignments', async (req: Request, res: Response, next) => {
for (let a2 of a1) {
assignments.push(a2);
finalcrew.push(symbol);
}
}
x++;
}

let apiResult = await VoyageTrackerAPI.postTrackedAssignmentsMany(dbid, finalcrew, assignments, getLogDataFromReq(req));
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
} catch (e) {
next(e);
}
});
Expand Down Expand Up @@ -392,21 +392,39 @@ router.get('/getTrackedData', async (req: Request, res: Response, next) => {
}
});

router.delete('/deleteTrackedData', async (req: Request, res: Response, next) => {
if (!req.query || (!req.query.dbid && !req.query.trackerId )) {
res.status(400).send('Whaat?');
return;
}

try {
let dbid = req.query?.dbid ? Number.parseInt(req.query.dbid.toString()) : undefined;
let trackerId = req.query?.trackerId ? Number.parseInt(req.query.trackerId.toString()) : undefined;
let apiResult = await VoyageTrackerAPI.deleteTrackedVoyage(dbid, trackerId);
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
next(e);
}
});



router.post('/postBossBattle', async (req: Request, res: Response, next) => {
if (!req.body) {
res.status(400).send('Whaat?');
return;
}
try {
}

try {
if ("id" in req.body) {
req.body.bossBattleId = req.body.id;
delete req.body.id;
}
let battle = req.body as IFBB_BossBattle_Document;
let apiResult = await CollaborationAPI.postBossBattle(battle);
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
} catch (e) {
next(e);
}
});
Expand All @@ -416,9 +434,9 @@ router.post('/postBossBattleSolves', async (req: Request, res: Response, next) =
if (!req.body) {
res.status(400).send('Whaat?');
return;
}
try {
}

try {
let fleetId = req.body.fleetId as number;
let bossBattleId = req.body.bossBattleId as number;
let chainIndex = req.body.chainIndex as number;
Expand All @@ -431,7 +449,7 @@ router.post('/postBossBattleSolves', async (req: Request, res: Response, next) =
let apiResult = await CollaborationAPI.postSolves(fleetId, bossBattleId, chainIndex, solves);

res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
} catch (e) {
next(e);
}
});
Expand All @@ -440,9 +458,9 @@ router.post('/postBossBattleTrials', async (req: Request, res: Response, next) =
if (!req.body) {
res.status(400).send('Whaat?');
return;
}
try {
}

try {
let fleetId = req.body.fleetId as number;
let bossBattleId = req.body.bossBattleId as number;
let chainIndex = req.body.chainIndex as number;
Expand All @@ -455,7 +473,7 @@ router.post('/postBossBattleTrials', async (req: Request, res: Response, next) =
let apiResult = await CollaborationAPI.postTrials(fleetId, bossBattleId, chainIndex, trials);

res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
} catch (e) {
next(e);
}
});
Expand All @@ -467,8 +485,8 @@ router.get('/getBossBattle', async (req: Request, res: Response, next) => {
return;
}

try {
let room = undefined as string | undefined;
try {
let room = undefined as string | undefined;
let id = undefined as number | undefined;
let fleetId: number | undefined = undefined;

Expand Down
28 changes: 28 additions & 0 deletions app/logic/voyagetracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ export class VoyageTracker extends VoyageTrackerBase {
return res;
}

protected async deleteVoyageByTrackerId(dbid: number, trackerId: number): Promise<boolean> {
let res: TrackedVoyage[] | null = null;
let result = false;

const sql = await makeSql(dbid);
if (sql) {
const repo = sql.getRepository(TrackedVoyage);
res = await repo.findAll({ where: { trackerId } });
if (res) {
for (let rec of res) {
rec.destroy();
}
result = true;
}

const crewrepo = sql.getRepository(TrackedCrew);
let crewres = await crewrepo.findAll({ where: { trackerId } });
if (crewres) {
for (let rec of crewres) {
rec.destroy();
}
result = true;
}
}

return result;
}

protected async getVoyagesByTrackerId(dbid: number, trackerId: number) {
let res: TrackedVoyage[] | null = null;

Expand Down
Loading

0 comments on commit f911b92

Please sign in to comment.