Skip to content

Commit

Permalink
add postTrackedData route.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Moschkin committed Sep 15, 2024
1 parent 41ddee9 commit 50cc497
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
56 changes: 56 additions & 0 deletions app/abstract/voyagetracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,55 @@ export abstract class VoyageTrackerBase {

}

async postTrackedData(
dbid: number,
voyage: ITrackedVoyage,
assignments: ITrackedAssignment[],
logData: LogData
): Promise<ApiResult> {
Logger.info("Tracked Voyage data", { dbid, voyage, logData });

const timeStamp = new Date();
let res: TrackerPostResult | null = null;

try {
res = await this.postOrPutTrackedData(dbid, voyage, assignments, timeStamp);
if (!res || res.status >= 300) {
return {
Status: res?.status ?? 500,
Body: {
dbid: dbid,
error: "Unable to insert record.",
timeStamp: timeStamp.toISOString(),
},
};
}
} catch (err) {
if (typeof err === "string") {
return {
Status: 500,
Body: err,
};
} else if (err instanceof Error) {
return {
Status: 500,
Body: err.toString(),
};
}
}

return {
Status: 201,
Body: {
dbid: dbid,
trackerId: res!.trackerId,
inputId: res!.inputId,
timeStamp: timeStamp.toISOString(),
},
};

}

async getTrackedVoyages(
dbid?: number,
trackerId?: number,
Expand Down Expand Up @@ -413,6 +462,13 @@ export abstract class VoyageTrackerBase {

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

protected abstract postOrPutTrackedData(
dbid: number,
voyage: ITrackedVoyage,
assigments: ITrackedAssignment[],
timeStamp?: Date
): Promise<TrackerPostResult>;

protected abstract postOrPutVoyage(
dbid: number,
voyage: ITrackedVoyage,
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ router.post('/postVoyage', async (req: Request, res: Response, next) => {
}
});

router.post('/postTrackedData', async (req: Request, res: Response, next) => {
if (!req.body || !req.body.dbid || !req.body.voyage || !req.body.assigments) {
res.status(400).send('Whaat?');
return;
}
try {
let dbid = req.body.dbid;
let voyage = req.body.voyage as ITrackedVoyage;
let assignments = req.body.assignments as ITrackedAssignment[];
let apiResult = await VoyageTrackerAPI.postTrackedData(dbid, voyage, assignments, getLogDataFromReq(req));
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
next(e);
}
});



router.get('/getVoyages', async (req: Request, res: Response, next) => {
if (!req.query || (!req.query.dbid && !req.query.trackerId )) {
res.status(400).send('Whaat?');
Expand Down
1 change: 1 addition & 0 deletions app/datacore/voyage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface ITrackedAssignment {
tracker_id: number;
slot: number; // Slot index where crew is seated
trait: string; // Matched trait or empty string if no match
crew?: string;
};

export interface ITrackedAssignmentsBySkill {
Expand Down
78 changes: 78 additions & 0 deletions app/logic/voyagetracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,84 @@ export class VoyageTracker extends VoyageTrackerBase {
return { status: 500 };
}


protected async postOrPutTrackedData(
dbid: number,
voyage: ITrackedVoyage,
assignments: ITrackedAssignment[],
timeStamp: Date = new Date()
): Promise<TrackerPostResult> {
const sql = await makeSql(dbid);

if (sql) {
const repo = sql.getRepository(TrackedVoyage);
let result: TrackedVoyage;

let current = await repo.findOne({ where: { trackerId: voyage.tracker_id } });
if (current) {
if (current.voyageId !== voyage.voyage_id) {
if (current.voyageId) {
return { status: 400 };
}
else if (!current.voyageId && voyage.voyage_id) {
current.voyageId = voyage.voyage_id;
}
}
current.voyage = voyage;
current.updatedAt = timeStamp;
result = await current.save();
}
else {
result = await repo.create({
dbid,
trackerId: voyage.tracker_id,
voyageId: voyage.voyage_id,
voyage,
timeStamp,
updatedAt: timeStamp
});

if (result && result.id !== result.trackerId) {
result.trackerId = result.id;
await result.save();
}
}

// sql?.close();
const retval = !!result?.id ? { status: 201, inputId: voyage.tracker_id, trackerId: result.id } : { status: 400 };
if (retval.status === 201 && retval.trackerId) {
const crewrepo = sql.getRepository(TrackedCrew);
if (crewrepo) {
let current = await crewrepo.findAll({ where: { trackerId: retval.trackerId! as number } });
if (current?.length) {
for (let rec of current) {
rec.destroy();
}
}

for (let assignment of assignments) {
assignment.tracker_id = retval.trackerId;
let result = await crewrepo.create({
dbid,
crew: assignment.crew,
trackerId: assignment.tracker_id,
assignment,
timeStamp,
});
if (!result?.id) {
retval.status = 500;
break;
}
}
}

return retval;
}
}

return { status: 500 };
}

protected async getAssignmentsByDbid(dbid: number, limit = 100) {
let res: TrackedCrew[] | null = null;

Expand Down

0 comments on commit 50cc497

Please sign in to comment.