Skip to content

Commit

Permalink
starting to add parcel service and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorFries committed Feb 20, 2024
1 parent 4d3375b commit efb0ef8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Empty file.
36 changes: 36 additions & 0 deletions express-api/src/services/parcels/parcelServices.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 17 additions & 0 deletions express-api/tests/unit/services/parcels/parcelsService.test.ts
Original file line number Diff line number Diff line change
@@ -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) => parcel);

describe('UNIT - Parcel Services', () => {
describe('addParcel', () => {
it('should add a new parcel and return it', async () => {

})
})
})

0 comments on commit efb0ef8

Please sign in to comment.