From efb0ef8068930805fcb3c01d6df99aa6db090b82 Mon Sep 17 00:00:00 2001 From: taylorfries Date: Tue, 20 Feb 2024 09:32:41 -0800 Subject: [PATCH] starting to add parcel service and unit test --- .../src/services/parcels/parcelSchema.ts | 0 .../src/services/parcels/parcelServices.ts | 36 +++++++++++++++++++ .../services/parcels/parcelsService.test.ts | 17 +++++++++ 3 files changed, 53 insertions(+) create mode 100644 express-api/src/services/parcels/parcelSchema.ts create mode 100644 express-api/src/services/parcels/parcelServices.ts create mode 100644 express-api/tests/unit/services/parcels/parcelsService.test.ts diff --git a/express-api/src/services/parcels/parcelSchema.ts b/express-api/src/services/parcels/parcelSchema.ts new file mode 100644 index 000000000..e69de29bb diff --git a/express-api/src/services/parcels/parcelServices.ts b/express-api/src/services/parcels/parcelServices.ts new file mode 100644 index 000000000..430ec6226 --- /dev/null +++ b/express-api/src/services/parcels/parcelServices.ts @@ -0,0 +1,36 @@ +import { Parcel } from "@/typeorm/Entities/Parcel"; +import { AppDataSource } from "@/appDataSource"; +import { ErrorWithCode } from "@/utilities/customErrors/ErrorWithCode"; + +const parcelRepo = AppDataSource.getRepository(Parcel); + +/** + * @description Adds a new parcel to the datasource. + * @param parcel incoming parcel data to be added to the database + * @returns {Response} A 201 status and the data of the role added. + * @throws ErrorWithCode If the parcel already exists or is unable to be added. + */ +export const postParcel = async (parcel: Parcel) => { + const existingParcel = await getParcelById(parcel.Id); + if (existingParcel) { + throw new ErrorWithCode('Parcel already exists', 409); + }; + const newParcel = parcelRepo.save(parcel); + return newParcel; +}; + +/** + * @description Finds and returns a parcel with matching Id + * @param parcelId Number representing parcel we want to find. + * @returns findParcel Parcel data matching Id passed in. + */ +export const getParcelById = async (parcelId: number) => { + try{ + const findParcel = await parcelRepo.findOne({ + where: { Id: parcelId}, + }); + return findParcel; + } catch (e) { + throw new ErrorWithCode(e.message, e.status); + } +} \ No newline at end of file diff --git a/express-api/tests/unit/services/parcels/parcelsService.test.ts b/express-api/tests/unit/services/parcels/parcelsService.test.ts new file mode 100644 index 000000000..ee836cbf9 --- /dev/null +++ b/express-api/tests/unit/services/parcels/parcelsService.test.ts @@ -0,0 +1,17 @@ +import { AppDataSource } from "@/appDataSource"; +import { Parcel } from "@/typeorm/Entities/Parcel"; +import { DeepPartial } from "typeorm"; + +const parcelRepo = AppDataSource.getRepository(Parcel); + +const _parcelSave = jest + .spyOn(parcelRepo, 'save') + .mockImplementation(async (parcel: DeepPartial & Parcel) => parcel); + + describe('UNIT - Parcel Services', () => { + describe('addParcel', () => { + it('should add a new parcel and return it', async () => { + + }) + }) + })