Skip to content

Commit

Permalink
#20: Throws error when receives data with datetime before the last one
Browse files Browse the repository at this point in the history
  • Loading branch information
williamquintas authored and arthursimas1 committed Jun 1, 2023
1 parent 4928666 commit e689fe3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
37 changes: 28 additions & 9 deletions pages/api/session/[sessionId]/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import admin from "../../../../config/firebase";
import { ICoordinatesData } from "../../../../models/ICoordinatesData";
import { CustomError, ErrorCode } from "../../../../utils/error";
import { createEntity } from "./entity";
import { getEntity } from "./entity/[entityId]";

Expand Down Expand Up @@ -54,18 +55,34 @@ const storeCoordinatesData = async (

await db.runTransaction(async (transaction) => {
const coordinatesCollection = sessionRef.collection("coordinates");
const coordinatesRef = coordinatesCollection.doc(
`${coordinatesData.date}T${coordinatesData.time}`
);

const { statusCode } = await getEntity(sessionId, coordinatesData.id);
if (statusCode === 404) {
await createEntity(sessionId, {
id: coordinatesData.id,
label: coordinatesData.id,
const [lastRecordDoc] = (
await coordinatesCollection.orderBy("date", "desc").limit(1).get()
).docs;
const lastRecordData = lastRecordDoc.data() as ICoordinatesData;

const isNewRecordBeforeLastOne =
lastRecordData.date >= coordinatesData.date &&
lastRecordData.time >= coordinatesData.time;
if (isNewRecordBeforeLastOne) {
throw new CustomError({
code: ErrorCode.DATE_AND_TIME_AFTER_PREVIOUS_ONE,
message: `New coordinates date and time should be after ${lastRecordDoc.id}.`,
});
} else {
const coordinatesRef = coordinatesCollection.doc(
`${coordinatesData.date}T${coordinatesData.time}`
);

const { statusCode } = await getEntity(sessionId, coordinatesData.id);
if (statusCode === 404) {
await createEntity(sessionId, {
id: coordinatesData.id,
label: coordinatesData.id,
});
}
transaction.create(coordinatesRef, coordinatesData);
}
transaction.create(coordinatesRef, coordinatesData);
});
};

Expand All @@ -86,6 +103,8 @@ const addCoordinateToSession = async (
const message = "Once one coordinate is allowed for each date and time.";
console.error(message, err);
return { statusCode: 400, message };
} else if (err.code === ErrorCode.DATE_AND_TIME_AFTER_PREVIOUS_ONE) {
return { statusCode: 400, message: err.message };
}
const message = `Error while adding coordinate to session ${sessionId}.`;
console.error(message, err);
Expand Down
13 changes: 13 additions & 0 deletions utils/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum ErrorCode {
DATE_AND_TIME_AFTER_PREVIOUS_ONE,
}

export class CustomError extends Error {
public code: ErrorCode;

constructor(err: { code: ErrorCode; message: string }) {
super();
this.code = err.code;
this.message = err.message;
}
}

0 comments on commit e689fe3

Please sign in to comment.