ORDER_UPDATE: async()

in packages/@prototype/simulator/src/SimulatorManagerStack/StatisticsLambda/@lambda/src/builders/order.js [40:69]


	ORDER_UPDATE: async (detail) => {
		const timestamp = Date.now()
		const { ID, state, provider, assignedAt, updatedAt } = detail
		let statusList = await client.keys(`${ORDER_STATUS}:*`)
		statusList = (statusList || []).map(q => q.split(':').pop())

		if (statusList.length === 0) {
			statusList = [state]
		}

		if (!statusList.includes(state)) {
			statusList.push(state)
		}

		const promises = statusList.filter(q => q !== 'all').map((s) => {
			if (s === state) {
				return client.hset(`${ORDER_STATUS}:${s}`, ID, timestamp)
			}

			return client.hdel(`${ORDER_STATUS}:${s}`, ID)
		})

		await Promise.all(promises)

		// If order has been delivered then add the count and sum duration
		if (state === 'DELIVERED') {
			await client.hincrby(`${PROVIDER_TIME}:${provider}`, 'orders', 1)
			await client.hincrby(`${PROVIDER_TIME}:${provider}`, 'duration', (updatedAt - assignedAt))
		}
	},