Throw a NotFoundException when container is not found
This commit is contained in:
parent
a15e24e901
commit
ea3d29ac68
|
|
@ -21,7 +21,7 @@ export class TrackingService {
|
|||
) {}
|
||||
|
||||
public async track(trackingInfo: TrackingDto) {
|
||||
const container = await this.containerRepository.findOneOrFail({
|
||||
const container = await this.containerRepository.findOne({
|
||||
relations: ['shipment'],
|
||||
where: {
|
||||
containerNumber: trackingInfo.containerNumber,
|
||||
|
|
@ -38,18 +38,22 @@ export class TrackingService {
|
|||
}
|
||||
|
||||
private validate(
|
||||
{ shipment }: ContainerEntity,
|
||||
container: ContainerEntity | null,
|
||||
trackingInfo: TrackingDto,
|
||||
): ShipmentEntity {
|
||||
if (!shipment) {
|
||||
if (!container) {
|
||||
throw new NotFoundException('Container not found');
|
||||
}
|
||||
|
||||
if (!container.shipment) {
|
||||
throw new NotFoundException('Shipment not found');
|
||||
}
|
||||
|
||||
if (shipment.carrierName !== trackingInfo.carrierName) {
|
||||
if (container.shipment.carrierName !== trackingInfo.carrierName) {
|
||||
throw new UnprocessableEntityException('CarrierName does not match');
|
||||
}
|
||||
|
||||
return shipment;
|
||||
return container.shipment;
|
||||
}
|
||||
|
||||
private async updateShipment(
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ describe('TrackingService', () => {
|
|||
switch (token) {
|
||||
case containerEntityToken:
|
||||
return {
|
||||
findOneOrFail: jest.fn().mockResolvedValue(mockContainerEntity),
|
||||
findOne: jest.fn().mockResolvedValue(mockContainerEntity),
|
||||
};
|
||||
case ShipmentsService:
|
||||
return {
|
||||
|
|
@ -55,7 +55,7 @@ describe('TrackingService', () => {
|
|||
|
||||
it('should have a track method that returns true', async () => {
|
||||
await expect(trackingService.track(mockTrackingInfo)).resolves.toBe(true);
|
||||
expect(containerEntityRepository.findOneOrFail).toHaveBeenCalledWith({
|
||||
expect(containerEntityRepository.findOne).toHaveBeenCalledWith({
|
||||
relations: ['shipment'],
|
||||
where: {
|
||||
containerNumber: mockTrackingInfo.containerNumber,
|
||||
|
|
@ -83,16 +83,21 @@ describe('TrackingService', () => {
|
|||
};
|
||||
|
||||
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);
|
||||
await expect(trackingService.track(mockTrackingInfo)).rejects.toThrow(
|
||||
'Shipment not found',
|
||||
);
|
||||
expect(notificationsService.send).toHaveBeenCalledTimes(0);
|
||||
expect(shipmentsService.update).toHaveBeenCalledTimes(0);
|
||||
|
||||
jest
|
||||
.spyOn(containerEntityRepository, 'findOneOrFail')
|
||||
.spyOn(containerEntityRepository, 'findOne')
|
||||
.mockResolvedValueOnce(mockShipmentCarrierNameMismatch);
|
||||
await expect(trackingService.track(mockTrackingInfo)).rejects.toThrow(
|
||||
'CarrierName does not match',
|
||||
|
|
|
|||
Loading…
Reference in New Issue