Skip to content

Commit c63883f

Browse files
created bonus feature for concurrent offer assignments
1 parent 07ef0ae commit c63883f

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

‎api/offer.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const __MODULE__ = 'Offer';
22
const { mountedResponse } = require('./utils/response');
3+
const brandService = require('./services/brandService');
34
const offerService = require('./services/offerService');
45
const locationService = require('./services/locationService');
56

@@ -51,3 +52,54 @@ module.exports.linkToLocation = async (event) => {
5152
const body = { message: 'Offer successfully linked to this Location' };
5253
return mountedResponse(body, 200);
5354
};
55+
56+
module.exports.linkAllBrandsLocationToAnOffer = async (event) => {
57+
const { offerId, brandId } = event.pathParameters;
58+
console.log(`${__MODULE__}@linkAllBrandsLocationToAnOffer: Assigning all locations from brand #${brandId} to offer #${offerId}`, event);
59+
60+
const offer = await offerService.getById(offerId);
61+
const locations = await brandService.getAllLocationsFromBrand(brandId);
62+
63+
console.debug(`${__MODULE__}@linkAllBrandsLocationToAnOffer: Offer found`, offer);
64+
console.debug(`${__MODULE__}@linkAllBrandsLocationToAnOffer: Locations found`, locations);
65+
66+
if (!offer) {
67+
const body = { message: 'Offer does not exists!' };
68+
69+
return mountedResponse(body, 404);
70+
}
71+
72+
if (locations && locations.length == 0) {
73+
const body = { message: 'No Locations were found for this brand!' };
74+
75+
return mountedResponse(body, 404);
76+
}
77+
78+
if (!locations) {
79+
const body = { message: 'An error ocurred while fetching all locations' };
80+
return mountedResponse(body, 500);
81+
}
82+
83+
try {
84+
const results = await Promise.allSettled(locations.forEach((location) => {
85+
await offerService.linkToLocation(offer, location);
86+
}));
87+
88+
const hasSomeFailure = results.some(result => result.status == 'rejected');
89+
90+
if (hasSomeFailure) {
91+
const body = { message: 'The action was completed, but not entirely successfull, some locations were not linked to this offer. Try again to link all them' };
92+
93+
return mountedResponse(body, 200);
94+
}
95+
96+
const body = { message: 'The action was successfully completed!' };
97+
98+
return mountedResponse(body, 200);
99+
100+
} catch (error) {
101+
const body = { message: 'Could not perform brands location assignment' };
102+
103+
return mountedResponse(body, 500);
104+
}
105+
};

‎api/services/brandService.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const uuid = require('uuid');
2+
const AWS = require('aws-sdk');
3+
4+
const dynamoDb = new AWS.DynamoDB.DocumentClient();
5+
6+
const __MODULE__ = 'BrandService';
7+
const { LOCATION_TABLE, BRAND_TABLE } = process.env
8+
9+
module.exports.create = async (brandName) => {
10+
const currentTimestamp = new Date().now;
11+
12+
const brand = {
13+
id: uuid.v1(),
14+
name: brandName,
15+
created: currentTimestamp,
16+
updated: currentTimestamp,
17+
};
18+
19+
const brandInfo = { TableName: BRAND_TABLE, Item: brand };
20+
21+
try {
22+
await dynamoDb.put(brandInfo).promise();
23+
24+
return brand;
25+
26+
} catch (error) {
27+
console.error(`${__MODULE__}@create: An error ocurred for brand creation`, error);
28+
29+
return false;
30+
}
31+
};
32+
33+
module.exports.getAllLocationsFromBrand = async (brandId) => {
34+
const locationQuery = {
35+
KeyConditionExpression: 'brandId = :brandId',
36+
ExpressionAttributeValues: {
37+
':brandId': { 'N': brandId },
38+
},
39+
TableName: LOCATION_TABLE,
40+
};
41+
42+
try {
43+
const { Items: locations } = await dynamoDb.query(locationQuery).promise();
44+
45+
if (!locations || locations.length < 1) {
46+
console.log(`${__MODULE__}@getAllLocationsFromBrand: No locations were found by brandId ${brandId}`);
47+
48+
return [];
49+
}
50+
51+
return locations;
52+
53+
} catch (error) {
54+
console.error(`${__MODULE__}@getAllLocationsFromBrand: An error ocurred for locations fetch`, error);
55+
56+
return null;
57+
}
58+
};

0 commit comments

Comments
 (0)