Skip to content

Commit d66c68a

Browse files
author
Paul Vatiche
committed
feat: add mcau
1 parent 8d8fe08 commit d66c68a

File tree

13 files changed

+909
-77
lines changed

13 files changed

+909
-77
lines changed

package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
"name": "mcserverless",
33
"version": "1.0.0",
44
"scripts": {
5-
"sls:mcall:local": "sls invoke local -c serverless.mcall.ts",
6-
"sls:mcall:deploy": "sls deploy -c serverless.mcall.ts",
5+
"mcall:local": "sls invoke local -c serverless.mcall.ts",
6+
"mcall:deploy": "sls deploy -c serverless.mcall.ts",
7+
"mcall:offline": "sls offline -c serverless.mcall.ts",
8+
"mcau:local": "sls invoke local -c serverless.mcau.ts",
9+
"mcau:deploy": "sls deploy -c serverless.mcau.ts",
10+
"mcau:offline": "sls offline -c serverless.mcau.ts",
711
"prepare": "husky install"
812
},
913
"dependencies": {
1014
"@aws-sdk/client-s3": "^3.409.0",
1115
"@prisma/client": "5.2.0",
1216
"@sailplane/logger": "^4.2.0",
1317
"p-queue": "^7.4.1",
14-
"prisma": "^5.2.0"
18+
"prisma": "^5.2.0",
19+
"serverless-offline": "^12.0.4"
1520
},
1621
"devDependencies": {
1722
"@serverless/typescript": "^3.30.1",

serverless.base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AWS } from '@serverless/typescript'
22

33
export const baseServerlessConfiguration: Partial<AWS> = {
44
frameworkVersion: '3',
5-
plugins: ['serverless-esbuild', 'serverless-dotenv-plugin'],
5+
plugins: ['serverless-esbuild', 'serverless-dotenv-plugin', 'serverless-offline'],
66
useDotenv: true,
77

88
package: {

serverless.mcau.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* eslint-disable @typescript-eslint/no-restricted-imports */
2+
import type { AWS } from '@serverless/typescript'
3+
import { baseServerlessConfiguration } from './serverless.base'
4+
5+
const serverlessConfiguration: AWS = {
6+
...baseServerlessConfiguration,
7+
service: 'mcau',
8+
9+
provider: {
10+
...baseServerlessConfiguration.provider,
11+
name: 'aws',
12+
region: 'ap-southeast-2',
13+
deploymentBucket: { name: "mcbrokenio-mcau-bucket-${opt:stage, 'dev'}" }
14+
},
15+
16+
functions: {
17+
getAllStores: {
18+
memorySize: 512,
19+
timeout: 900,
20+
handler: 'src/stacks/mcau/getAllStores.handler',
21+
events: [
22+
{
23+
schedule: { rate: ['cron(05 1 ? * SUN *)'], input: { countries: ['AU2'] } }
24+
// schedule: { rate: ['cron(17 * ? * * *)'], input: { countries: ['AU2'] } }
25+
},
26+
{
27+
schedule: { rate: ['cron(05 2 ? * SUN *)'], input: { countries: ['AU'] } }
28+
29+
}
30+
],
31+
environment: {
32+
DATABASE_URL: '${env:DATABASE_URL}',
33+
BASIC_TOKEN_AP: '${env:BASIC_TOKEN_AP}'
34+
}
35+
},
36+
getItemStatus: {
37+
memorySize: 368,
38+
timeout: 900,
39+
handler: 'src/stacks/mcau/getItemStatus.handler',
40+
events: [
41+
{
42+
schedule: 'cron(05 0 ? * SUN *)'
43+
}
44+
],
45+
environment: {
46+
BASIC_TOKEN_AP: '${env:BASIC_TOKEN_AP}',
47+
DATABASE_URL: '${env:DATABASE_URL}'
48+
}
49+
}
50+
},
51+
52+
custom: {
53+
esbuild: {
54+
bundle: true,
55+
minify: false,
56+
sourcemap: true,
57+
exclude: ['aws-sdk'],
58+
target: 'node18',
59+
define: { 'require.resolve': undefined },
60+
platform: 'node',
61+
concurrency: 10
62+
}
63+
}
64+
}
65+
66+
module.exports = serverlessConfiguration

src/libs/constants/CountryInfos.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,19 @@ export const CountryInfos: Record<Locations, ICountryInfos> = {
900900
[IceType.MCFLURRY]: ['5739', '5738'],
901901
[IceType.MCSUNDAE]: ['30001', '30002', '30003', '30004']
902902
},
903+
customItems: {
904+
fantaRaspberry: ['2337', '2338', '2339'],
905+
fantaMango: ['3319', '3336', '3534'],
906+
fantaGrape: ['3474', '3475', '3480'],
907+
fantaBlueberry: ['3409', '3410', '3414'],
908+
frozenCoke: ['265', '266', '6081'],
909+
frozenCokeZero: ['15029', '15030', '15031']
910+
},
903911
locationLimits: {
904-
minLatitude: -43.003,
905-
maxLatitude: -10.8752,
906-
minLongitude: 112.911,
907-
maxLongitude: 153.6388
912+
minLatitude: -23.648618,
913+
maxLatitude: -9.219482,
914+
minLongitude: 112.9211,
915+
maxLongitude: 153.6382
908916
}
909917
},
910918
[Locations.AU2]: {
@@ -919,11 +927,19 @@ export const CountryInfos: Record<Locations, ICountryInfos> = {
919927
[IceType.MCFLURRY]: ['5739', '5738'],
920928
[IceType.MCSUNDAE]: ['30001', '30002', '30003', '30004']
921929
},
930+
customItems: {
931+
fantaRaspberry: ['2337', '2338', '2339'],
932+
fantaMango: ['3319', '3336', '3534'],
933+
fantaGrape: ['3474', '3475', '3480'],
934+
fantaBlueberry: ['3409', '3410', '3414'],
935+
frozenCoke: ['265', '266', '6081'],
936+
frozenCokeZero: ['15029', '15030', '15031']
937+
},
922938
locationLimits: {
923-
minLatitude: -35.0,
924-
maxLatitude: -10.8752,
925-
minLongitude: 112.911,
926-
maxLongitude: 129.7749
939+
minLatitude: -43.648618,
940+
maxLatitude: -23.648618,
941+
minLongitude: 112.9211,
942+
maxLongitude: 153.6382
927943
}
928944
},
929945

src/libs/constants/RateLimit.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export interface RequestLimiter {
55
}
66

77
export const defaultRequestLimiterAu = {
8-
maxRequestsPerSecond: 11,
8+
maxRequestsPerSecond: 6,
99
requestsPerLog: 500,
10-
concurrentRequests: 15
10+
concurrentRequests: 8
1111
}
1212

1313
export const defaultRequestLimiterEu = {
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import axios from 'axios'
22
import { type CreatePos, type ICountryInfos, type ILocation } from '@libs/types'
33
import { type IRestaurantLocationResponse } from '@libs/types/responses'
4-
import { Logger } from '@sailplane/logger'
54
import { randomUserAgent } from '@libs/utils/randomUserAgent'
65

7-
const logger = new Logger('getStoreListForLocation')
8-
96
export async function getStorelistFromLocation(
107
{ latitude, longitude }: ILocation,
118
{ country, getStores: { url, mobileString } }: ICountryInfos,
129
token: string,
1310
clientId: string
1411
) {
15-
try {
16-
const {
17-
data: { response }
18-
} = await axios.get<IRestaurantLocationResponse>(
12+
const {
13+
data: { response }
14+
} = await axios.get<IRestaurantLocationResponse>(
1915
`${url}latitude=${latitude}&longitude=${longitude}`,
2016
{
2117
headers: {
@@ -27,40 +23,23 @@ export async function getStorelistFromLocation(
2723
'accept-language': country === 'UK' ? 'en-GB' : 'de-DE'
2824
}
2925
}
30-
)
31-
32-
const posArray: CreatePos[] = response.restaurants.map((restaurant) => {
33-
const pos: CreatePos = {
34-
id: `${country}-${restaurant.nationalStoreNumber}`,
35-
nationalStoreNumber: `${restaurant.nationalStoreNumber}`,
36-
name: restaurant.name,
37-
hasMobileOrdering: mobileString != null
38-
? restaurant.facilities.includes(mobileString)
39-
: false,
40-
latitude: restaurant.location.latitude.toString(),
41-
longitude: restaurant.location.longitude.toString(),
42-
country
43-
}
44-
45-
return pos
46-
})
47-
48-
return posArray
49-
} catch (error) {
50-
if (axios.isAxiosError(error)) {
51-
const axiosError = error
52-
53-
if (axiosError.response?.status === 401) {
54-
logger.warn('Bad request error')
55-
} else {
56-
logger.error(
57-
`Error while getting stores for location ${latitude}, ${longitude} in ${country}: ${axiosError.response?.statusText}`
58-
)
59-
}
26+
)
27+
28+
const posArray: CreatePos[] = response.restaurants.map((restaurant) => {
29+
const pos: CreatePos = {
30+
id: `${country}-${restaurant.nationalStoreNumber}`,
31+
nationalStoreNumber: `${restaurant.nationalStoreNumber}`,
32+
name: restaurant.name,
33+
hasMobileOrdering: mobileString != null
34+
? restaurant.facilities.includes(mobileString)
35+
: false,
36+
latitude: restaurant.location.latitude.toString(),
37+
longitude: restaurant.location.longitude.toString(),
38+
country
6039
}
6140

62-
logger.error('Error while getting stores for location, no axios error')
63-
}
41+
return pos
42+
})
6443

65-
return [] as CreatePos[]
44+
return posArray
6645
}

src/libs/services/getAllStores/getStoreListLocation/index.ts

+35-18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Logger } from '@sailplane/logger'
1212
import { defaultRequestLimiterAu } from '@libs/constants/RateLimit'
1313
import { savePos } from '@libs/services/getAllStores/savePos'
1414
import { getStorelistFromLocation } from '@libs/services/getAllStores/getStoreListLocation/getStorelistFromLocation'
15+
import axios from 'axios'
1516

1617
const logger = new Logger({
1718
logTimestamps: true,
@@ -42,6 +43,7 @@ export async function getStorelistWithLocation(
4243
const maxRequestsPerSecond = requestLimiter.maxRequestsPerSecond
4344
let requestsThisSecond = 0
4445
let totalRequestsProcessed = 0
46+
let errorCount = 0
4547

4648
async function processLocation(
4749
location: ILocation,
@@ -54,26 +56,39 @@ export async function getStorelistWithLocation(
5456

5557
requestsThisSecond++
5658

57-
const pos = await getStorelistFromLocation(
58-
location,
59-
countryInfo,
60-
token,
61-
clientId
62-
)
63-
64-
if (pos.length > 0) {
65-
posArray.push(pos)
66-
pos.forEach((p) => {
67-
if (!posMap.has(p.nationalStoreNumber)) {
68-
posMap.set(p.nationalStoreNumber, p)
59+
try {
60+
const pos = await getStorelistFromLocation(
61+
location,
62+
countryInfo,
63+
token,
64+
clientId
65+
)
66+
67+
if (pos.length > 0) {
68+
posArray.push(pos)
69+
pos.forEach((p) => {
70+
if (!posMap.has(p.nationalStoreNumber)) {
71+
posMap.set(p.nationalStoreNumber, p)
72+
}
73+
})
74+
}
75+
76+
totalRequestsProcessed++
77+
78+
if (totalRequestsProcessed % requestsPerLog === 0) {
79+
logger.debug(`Processed ${totalRequestsProcessed}`)
80+
}
81+
} catch (error) {
82+
errorCount++
83+
if (axios.isAxiosError(error)) {
84+
const axiosError = error
85+
86+
if (axiosError.response?.status === 401) {
87+
logger.warn('Bad request error')
6988
}
70-
})
71-
}
89+
}
7290

73-
totalRequestsProcessed++
74-
75-
if (totalRequestsProcessed % requestsPerLog === 0) {
76-
logger.debug(`Processed ${totalRequestsProcessed}`)
91+
// logger.error('Error while getting stores for location, no axios error')
7792
}
7893
}
7994

@@ -100,6 +115,8 @@ export async function getStorelistWithLocation(
100115

101116
await Promise.all(asyncTasks)
102117

118+
logger.info(`ErrorCount ${errorCount}`)
119+
103120
const uniquePosArray = Array.from(posMap.values())
104121

105122
await savePos(uniquePosArray)

0 commit comments

Comments
 (0)