Throw a NotFoundException when container is not found

This commit is contained in:
Thom Werring 2024-03-26 22:28:36 +01:00
parent a15e24e901
commit ea3d29ac68
2 changed files with 20 additions and 11 deletions

View File

@ -21,7 +21,7 @@ export class TrackingService {
) {} ) {}
public async track(trackingInfo: TrackingDto) { public async track(trackingInfo: TrackingDto) {
const container = await this.containerRepository.findOneOrFail({ const container = await this.containerRepository.findOne({
relations: ['shipment'], relations: ['shipment'],
where: { where: {
containerNumber: trackingInfo.containerNumber, containerNumber: trackingInfo.containerNumber,
@ -38,18 +38,22 @@ export class TrackingService {
} }
private validate( private validate(
{ shipment }: ContainerEntity, container: ContainerEntity | null,
trackingInfo: TrackingDto, trackingInfo: TrackingDto,
): ShipmentEntity { ): ShipmentEntity {
if (!shipment) { if (!container) {
throw new NotFoundException('Container not found');
}
if (!container.shipment) {
throw new NotFoundException('Shipment not found'); throw new NotFoundException('Shipment not found');
} }
if (shipment.carrierName !== trackingInfo.carrierName) { if (container.shipment.carrierName !== trackingInfo.carrierName) {
throw new UnprocessableEntityException('CarrierName does not match'); throw new UnprocessableEntityException('CarrierName does not match');
} }
return shipment; return container.shipment;
} }
private async updateShipment( private async updateShipment(

View File

@ -26,7 +26,7 @@ describe('TrackingService', () => {
switch (token) { switch (token) {
case containerEntityToken: case containerEntityToken:
return { return {
findOneOrFail: jest.fn().mockResolvedValue(mockContainerEntity), findOne: jest.fn().mockResolvedValue(mockContainerEntity),
}; };
case ShipmentsService: case ShipmentsService:
return { return {
@ -55,7 +55,7 @@ describe('TrackingService', () => {
it('should have a track method that returns true', async () => { it('should have a track method that returns true', async () => {
await expect(trackingService.track(mockTrackingInfo)).resolves.toBe(true); await expect(trackingService.track(mockTrackingInfo)).resolves.toBe(true);
expect(containerEntityRepository.findOneOrFail).toHaveBeenCalledWith({ expect(containerEntityRepository.findOne).toHaveBeenCalledWith({
relations: ['shipment'], relations: ['shipment'],
where: { where: {
containerNumber: mockTrackingInfo.containerNumber, containerNumber: mockTrackingInfo.containerNumber,
@ -83,16 +83,21 @@ describe('TrackingService', () => {
}; };
jest jest
.spyOn(containerEntityRepository, 'findOneOrFail') .spyOn(containerEntityRepository, 'findOne')
.mockResolvedValueOnce(null);
await expect(trackingService.track(mockTrackingInfo)).rejects.toThrow(
'Container not found',
);
jest
.spyOn(containerEntityRepository, 'findOne')
.mockResolvedValueOnce(mockShipmentNotFound); .mockResolvedValueOnce(mockShipmentNotFound);
await expect(trackingService.track(mockTrackingInfo)).rejects.toThrow( await expect(trackingService.track(mockTrackingInfo)).rejects.toThrow(
'Shipment not found', 'Shipment not found',
); );
expect(notificationsService.send).toHaveBeenCalledTimes(0);
expect(shipmentsService.update).toHaveBeenCalledTimes(0);
jest jest
.spyOn(containerEntityRepository, 'findOneOrFail') .spyOn(containerEntityRepository, 'findOne')
.mockResolvedValueOnce(mockShipmentCarrierNameMismatch); .mockResolvedValueOnce(mockShipmentCarrierNameMismatch);
await expect(trackingService.track(mockTrackingInfo)).rejects.toThrow( await expect(trackingService.track(mockTrackingInfo)).rejects.toThrow(
'CarrierName does not match', 'CarrierName does not match',