Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit fca18e5

Browse files
committed
setup method to match shipments with driver
1 parent beaf11f commit fca18e5

File tree

5 files changed

+94
-5
lines changed

5 files changed

+94
-5
lines changed

‎src/app.module.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { MongooseModule } from '@nestjs/mongoose';
55
import { DatabaseService } from 'services/database.service';
66
import { FindShipmentModule } from './find-shipment/find-shipment.module';
77
import { AcceptShipmentsModule } from './accept-shipments/accept-shipments.module';
8+
import { CalculateShipmentsService } from 'services/calculate-shipments.service';
89

910

1011
@Module({
1112
imports: [MongooseModule.forRoot('mongodb://localhost:27017/quickload'), FindShipmentModule, AcceptShipmentsModule],
1213
controllers: [AppController],
13-
providers: [AppService, DatabaseService],
14+
providers: [AppService, DatabaseService,CalculateShipmentsService],
1415
})
1516
export class AppModule {}

‎src/find-shipment/find-shipment.controller.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { CalculateShipmentsService } from 'services/calculate-shipments.service';
12
import { Drivers } from './../interfaces/drivers';
23
import { DatabaseService } from 'services/database.service';
34
import { Controller, Res, Post, Body } from '@nestjs/common';
45

56
@Controller('find-shipment')
67
export class FindShipmentController {
7-
constructor(private db: DatabaseService) {}
8+
constructor(
9+
private db: DatabaseService,
10+
private calculate: CalculateShipmentsService,
11+
) {}
812

913
@Post()
1014
async getShipments(@Body() body, @Res() res) {
@@ -13,8 +17,12 @@ export class FindShipmentController {
1317
} else {
1418
await this.db
1519
.findAllShipments()
16-
.then(response => {
17-
res.status(202).send(response);
20+
.then(async response => {
21+
await this.calculate.matchDriver(response, body).then((xx) => {
22+
res.status(202).send(xx);
23+
}).catch((err) => {
24+
res.status(204).send({message: 'Error with calculations: ' + err});
25+
});
1826
})
1927
.catch(err => {
2028
res.status(204).send(err);

‎src/find-shipment/find-shipment.module.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CalculateShipmentsService } from 'services/calculate-shipments.service';
12
import { DatabaseService } from 'services/database.service';
23
import { ShipmentSchema } from './../schemas/shipment-schema/shipment-schema';
34
import { MongooseModule } from '@nestjs/mongoose';
@@ -13,6 +14,6 @@ import { ShipmentLogSchema } from 'schemas/shipment-log/shipment-log-schema';
1314
{ name: 'ShipmentLog', schema: ShipmentLogSchema}]),
1415
],
1516
controllers: [FindShipmentController],
16-
providers: [DatabaseService],
17+
providers: [DatabaseService,CalculateShipmentsService],
1718
})
1819
export class FindShipmentModule {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { CalculateShipmentsService } from './calculate-shipments.service';
3+
4+
describe('CalculateShipmentsService', () => {
5+
let service: CalculateShipmentsService;
6+
beforeAll(async () => {
7+
const module: TestingModule = await Test.createTestingModule({
8+
providers: [CalculateShipmentsService],
9+
}).compile();
10+
service = module.get<CalculateShipmentsService>(CalculateShipmentsService);
11+
});
12+
it('should be defined', () => {
13+
expect(service).toBeDefined();
14+
});
15+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Drivers } from './../interfaces/drivers';
2+
import { Injectable } from '@nestjs/common';
3+
4+
@Injectable()
5+
export class CalculateShipmentsService {
6+
constructor() {}
7+
8+
async matchDriver(shipments, driver: Drivers) {
9+
return await new Promise((resolve, reject) => {
10+
let x = [];
11+
12+
// check if origin is same
13+
x = shipments.filter(item => driver.Origin === item.Origin);
14+
15+
// if destination, check if dest is same
16+
if (driver.Destination) {
17+
x = x.filter(item => item.Destination === driver.Destination);
18+
}
19+
20+
// check if refrigerated
21+
if (driver.TruckType.Type === 'Refrigerator') {
22+
// filter types refrigerator
23+
x = x.filter(
24+
item =>
25+
item.RequiredTruckInformation.RequiredTruckType[0] ===
26+
'Refrigerator',
27+
);
28+
// check length
29+
x = x.filter(
30+
item =>
31+
item.RequiredTruckInformation.Length <= driver.TruckType.Length,
32+
);
33+
// check weight
34+
x = x.filter(
35+
item =>
36+
item.RequiredTruckInformation.Weight <= driver.TruckType.Weight,
37+
);
38+
} else {
39+
// check for specific truck type
40+
x = x.filter( (item) => {
41+
return item.RequiredTruckInformation.RequiredTruckType.some((params) => {
42+
return params === driver.TruckType.Type;
43+
});
44+
});
45+
// check length
46+
x = x.filter(
47+
item =>
48+
item.RequiredTruckInformation.Length <= driver.TruckType.Length,
49+
);
50+
// check weight
51+
x = x.filter(
52+
item =>
53+
item.RequiredTruckInformation.Weight <= driver.TruckType.Weight,
54+
);
55+
}
56+
57+
if (x.length === 0) {
58+
reject('No Entries Found');
59+
} else {
60+
resolve(x);
61+
}
62+
});
63+
}
64+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /