-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starting to add parcel service and unit test
- Loading branch information
1 parent
4d3375b
commit efb0ef8
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
express-api/tests/unit/services/parcels/parcelsService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
|
||
}) | ||
}) | ||
}) |