From 1932e0645bb2af73a18ff077d7af10165dcffd9e Mon Sep 17 00:00:00 2001 From: Nevil Date: Wed, 15 Feb 2023 15:32:44 +0530 Subject: [PATCH 1/4] added session update api and on_update callback --- src/controllers/index.js | 11 +++++ src/database/storage/bap/queries.js | 8 +++ .../storage/sessionAttendance/queries.js | 7 +++ src/database/storage/user/queries.js | 8 +++ src/dtos/onUpdateRequest.js | 15 ++++++ src/routes/index.js | 1 + src/services/protocolCallbacks/index.js | 4 +- src/services/protocolCallbacks/onUpdate.js | 33 +++++++++++++ src/services/session.js | 49 +++++++++++++++++++ 9 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/dtos/onUpdateRequest.js create mode 100644 src/services/protocolCallbacks/onUpdate.js create mode 100644 src/services/session.js diff --git a/src/controllers/index.js b/src/controllers/index.js index 2f0f1ff..c75424c 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -6,6 +6,7 @@ const confirmService = require('@services/apis/confirm') const selectService = require('@services/apis/select') const statusService = require('@services/apis/status') const cancelService = require('@services/apis/cancel') +const sessionService = require('@services/session') exports.search = async (req, res) => { try { @@ -61,3 +62,13 @@ exports.status = async (req, res) => { console.log(err) } } + +exports.sessionUpdate = async (req, res) => { + try { + console.debug(JSON.stringify(req.body, null, '\t')) + res.status(200).send(responses.success_ack) + await sessionService.session(req.body) + } catch (err) { + console.log(err) + } +} diff --git a/src/database/storage/bap/queries.js b/src/database/storage/bap/queries.js index 26a5771..3a95fed 100644 --- a/src/database/storage/bap/queries.js +++ b/src/database/storage/bap/queries.js @@ -27,3 +27,11 @@ exports.findOrCreate = async ({ where = {}, defaults = {} }) => { throw err } } + +exports.findByIds = async (ids) => { + try { + return await Bap.find({ _id: { $in: ids } }).lean() + } catch (err) { + console.log(err) + } +} diff --git a/src/database/storage/sessionAttendance/queries.js b/src/database/storage/sessionAttendance/queries.js index c9e203e..658e056 100644 --- a/src/database/storage/sessionAttendance/queries.js +++ b/src/database/storage/sessionAttendance/queries.js @@ -55,3 +55,10 @@ exports.setStatusAsCancelledById = async (id, { reasonId, reasonDesc }) => { console.log('SessionAttendance.findByOrderId: ', err) } } +exports.findBySessionId = async (sessionId) => { + try { + return await SessionAttendance.find({ sessionId: sessionId }) + } catch (err) { + console.log('SessionAttendance.findBySessionId: ', err) + } +} diff --git a/src/database/storage/user/queries.js b/src/database/storage/user/queries.js index 973934e..25eef08 100644 --- a/src/database/storage/user/queries.js +++ b/src/database/storage/user/queries.js @@ -35,3 +35,11 @@ exports.findOrCreate = async ({ where = {}, defaults = {} }) => { throw err } } + +exports.findByIds = async (ids) => { + try { + return await User.find({ _id: { $in: ids } }).lean() + } catch (err) { + console.log(err) + } +} diff --git a/src/dtos/onUpdateRequest.js b/src/dtos/onUpdateRequest.js new file mode 100644 index 0000000..022ce3f --- /dev/null +++ b/src/dtos/onUpdateRequest.js @@ -0,0 +1,15 @@ +'use strict' + +exports.onUpdateRequestDTO = async (context, statusBody, orderId, status) => { + return { + context, + message: { + order: { + id: orderId, + state: status, + type: 'DEFAULT', + provider: statusBody, + }, + }, + } +} diff --git a/src/routes/index.js b/src/routes/index.js index 2cc93d0..6479483 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -10,5 +10,6 @@ router.post('/init', bppController.init) router.post('/confirm', bppController.confirm) router.post('/cancel', bppController.cancel) router.post('/status', bppController.status) +router.post('/sessionUpdate', bppController.sessionUpdate) module.exports = router diff --git a/src/services/protocolCallbacks/index.js b/src/services/protocolCallbacks/index.js index 745710c..6ee1d80 100644 --- a/src/services/protocolCallbacks/index.js +++ b/src/services/protocolCallbacks/index.js @@ -4,5 +4,7 @@ const { onConfirm } = require('./onConfirm') const { onSelect } = require('./onSelect') const { onStatus } = require('./onStatus') const { onCancel } = require('./onCancel') -const protocolCallbacks = { onConfirm, onSelect, onStatus, onCancel } +const { onUpdate } = require('./onUpdate') + +const protocolCallbacks = { onConfirm, onSelect, onStatus, onCancel, onUpdate } module.exports = protocolCallbacks diff --git a/src/services/protocolCallbacks/onUpdate.js b/src/services/protocolCallbacks/onUpdate.js new file mode 100644 index 0000000..28b5dfb --- /dev/null +++ b/src/services/protocolCallbacks/onUpdate.js @@ -0,0 +1,33 @@ +'use strict' + +const { contextBuilder } = require('@utils/contextBuilder') +const { onUpdateRequestDTO } = require('@dtos/onUpdateRequest') +const { postRequest } = require('@utils/requester') +const { internalRequests } = require('@helpers/requests') + +exports.onUpdate = async (callbackData) => { + try { + const context = await contextBuilder( + callbackData.transactionId, + callbackData.messageId, + process.env.ON_UPDATE_ACTION + ) + const response = await internalRequests.catalogGET({ + route: process.env.CATALOG_GET_STATUS_BODY_ROUTE, + pathParams: { + sessionId: callbackData.sessionId, + fulfillmentId: callbackData.fulfillmentId, + }, + }) + const statusBody = response.statusBody + const onUpdateRequest = await onUpdateRequestDTO( + context, + statusBody.providers[0], + callbackData.orderId, + callbackData.status + ) + await postRequest(callbackData.bapUri, process.env.ON_UPDATE_ROUTE, {}, onUpdateRequest, { shouldSign: false }) + } catch (err) { + console.log('OnUpdate.ProtocolCallbacks.services: ', err) + } +} diff --git a/src/services/session.js b/src/services/session.js new file mode 100644 index 0000000..40cff18 --- /dev/null +++ b/src/services/session.js @@ -0,0 +1,49 @@ +'use strict' + +const protocolCallbacks = require('@services/protocolCallbacks/') +const sessionAttendanceQueries = require('@database/storage/sessionAttendance/queries') +const userQueries = require('@database/storage/user/queries') +const bapQueries = require('@database/storage/bap/queries') +const crypto = require('crypto') +const { getRequest } = require('@utils/requester') + +exports.session = async (requestBody) => { + try { + const sessionId = requestBody.sessionId + const sessionAttendanceDoc = await sessionAttendanceQueries.findBySessionId(sessionId) + if (!sessionAttendanceDoc) return console.log('SessionAttendance Not Found') + const userIds = sessionAttendanceDoc.map((sessionAttendee) => { + console.log(sessionAttendee.userId) + return sessionAttendee.userId + }) + const userDocs = await userQueries.findByIds(userIds) + if (!userDocs) return console.log('Users Not Found') + const bapIds = userDocs.map((user) => { + console.log(user.bapId) + return user.bapId + }) + const bapDocs = await bapQueries.findByIds(bapIds) + if (!bapDocs) return console.log('BAP Not Found') + + let usersWithBapMeta = userDocs.map((item) => ({ + ...item, + bapInfo: bapDocs.filter((f) => f._id.toString() == item.bapId.toString()), + sessionAttendance: sessionAttendanceDoc.filter((f) => f.userId.toString() == item._id.toString()), + })) + + usersWithBapMeta.map(async function (user) { + await protocolCallbacks.onUpdate({ + transactionId: crypto.randomUUID(), + messageId: crypto.randomUUID(), + bapId: user.bapInfo[0].bapId, + bapUri: user.bapInfo[0].bapUri, + status: user.sessionAttendance[0].statusText, + sessionId: sessionId.toString(), + fulfillmentId: user.sessionAttendance[0].fulfillmentId.toString(), + orderId: user.sessionAttendance[0].orderId.toString(), + }) + }) + } catch (err) { + console.log(err) + } +} From 9685cef241e2695567a1d6a4b810ac2545b2e203 Mon Sep 17 00:00:00 2001 From: Nevil Date: Wed, 15 Feb 2023 15:35:36 +0530 Subject: [PATCH 2/4] updated session service --- src/services/session.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/session.js b/src/services/session.js index 40cff18..838f785 100644 --- a/src/services/session.js +++ b/src/services/session.js @@ -5,7 +5,6 @@ const sessionAttendanceQueries = require('@database/storage/sessionAttendance/qu const userQueries = require('@database/storage/user/queries') const bapQueries = require('@database/storage/bap/queries') const crypto = require('crypto') -const { getRequest } = require('@utils/requester') exports.session = async (requestBody) => { try { From 31a1612841d968191ca91f7482017b0e50d4d66d Mon Sep 17 00:00:00 2001 From: Nevil Date: Fri, 17 Feb 2023 01:01:32 +0530 Subject: [PATCH 3/4] updated variables and POST req --- src/services/protocolCallbacks/onUpdate.js | 8 +++-- src/services/session.js | 39 +++++++++++----------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/services/protocolCallbacks/onUpdate.js b/src/services/protocolCallbacks/onUpdate.js index 28b5dfb..5f21858 100644 --- a/src/services/protocolCallbacks/onUpdate.js +++ b/src/services/protocolCallbacks/onUpdate.js @@ -2,7 +2,7 @@ const { contextBuilder } = require('@utils/contextBuilder') const { onUpdateRequestDTO } = require('@dtos/onUpdateRequest') -const { postRequest } = require('@utils/requester') +const { externalRequests } = require('@helpers/requests') const { internalRequests } = require('@helpers/requests') exports.onUpdate = async (callbackData) => { @@ -26,7 +26,11 @@ exports.onUpdate = async (callbackData) => { callbackData.orderId, callbackData.status ) - await postRequest(callbackData.bapUri, process.env.ON_UPDATE_ROUTE, {}, onUpdateRequest, { shouldSign: false }) + await externalRequests.callbackPOST({ + baseURL: callbackData.bapUri, + route: process.env.ON_UPDATE_ROUTE, + body: onUpdateRequest, + }) } catch (err) { console.log('OnUpdate.ProtocolCallbacks.services: ', err) } diff --git a/src/services/session.js b/src/services/session.js index 838f785..c87199a 100644 --- a/src/services/session.js +++ b/src/services/session.js @@ -9,37 +9,36 @@ const crypto = require('crypto') exports.session = async (requestBody) => { try { const sessionId = requestBody.sessionId - const sessionAttendanceDoc = await sessionAttendanceQueries.findBySessionId(sessionId) - if (!sessionAttendanceDoc) return console.log('SessionAttendance Not Found') - const userIds = sessionAttendanceDoc.map((sessionAttendee) => { - console.log(sessionAttendee.userId) + const sessionAttendance = await sessionAttendanceQueries.findBySessionId(sessionId) + if (!sessionAttendance) return console.log('SessionAttendance Not Found') + const userIds = sessionAttendance.map((sessionAttendee) => { return sessionAttendee.userId }) - const userDocs = await userQueries.findByIds(userIds) - if (!userDocs) return console.log('Users Not Found') - const bapIds = userDocs.map((user) => { - console.log(user.bapId) + const users = await userQueries.findByIds(userIds) + if (!users) return console.log('Users Not Found') + + const bapIds = users.map((user) => { return user.bapId }) - const bapDocs = await bapQueries.findByIds(bapIds) - if (!bapDocs) return console.log('BAP Not Found') + const baps = await bapQueries.findByIds(bapIds) + if (!baps) return console.log('BAP Not Found') - let usersWithBapMeta = userDocs.map((item) => ({ - ...item, - bapInfo: bapDocs.filter((f) => f._id.toString() == item.bapId.toString()), - sessionAttendance: sessionAttendanceDoc.filter((f) => f.userId.toString() == item._id.toString()), + let usersWithBapAndAttendance = users.map((user) => ({ + ...user, + bapInfo: baps.find((f) => f._id.toString() == user.bapId.toString()), + sessionAttendance: sessionAttendance.find((f) => f.userId.toString() == user._id.toString()), })) - usersWithBapMeta.map(async function (user) { + usersWithBapAndAttendance.map(async function (user) { await protocolCallbacks.onUpdate({ transactionId: crypto.randomUUID(), messageId: crypto.randomUUID(), - bapId: user.bapInfo[0].bapId, - bapUri: user.bapInfo[0].bapUri, - status: user.sessionAttendance[0].statusText, + bapId: user.bapInfo.bapId, + bapUri: user.bapInfo.bapUri, + status: user.sessionAttendance.statusText, sessionId: sessionId.toString(), - fulfillmentId: user.sessionAttendance[0].fulfillmentId.toString(), - orderId: user.sessionAttendance[0].orderId.toString(), + fulfillmentId: user.sessionAttendance.fulfillmentId.toString(), + orderId: user.sessionAttendance.orderId.toString(), }) }) } catch (err) { From 8b0631f37402ba903ba25a6a556503f426580f58 Mon Sep 17 00:00:00 2001 From: Nevil Date: Fri, 17 Feb 2023 01:09:47 +0530 Subject: [PATCH 4/4] optimized the code --- src/services/session.js | 45 ++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/services/session.js b/src/services/session.js index c87199a..e6e49cc 100644 --- a/src/services/session.js +++ b/src/services/session.js @@ -10,35 +10,38 @@ exports.session = async (requestBody) => { try { const sessionId = requestBody.sessionId const sessionAttendance = await sessionAttendanceQueries.findBySessionId(sessionId) - if (!sessionAttendance) return console.log('SessionAttendance Not Found') - const userIds = sessionAttendance.map((sessionAttendee) => { - return sessionAttendee.userId - }) + if (!sessionAttendance) { + return console.log('SessionAttendance Not Found') + } + const userIds = sessionAttendance.map((sessionAttendee) => sessionAttendee.userId) const users = await userQueries.findByIds(userIds) - if (!users) return console.log('Users Not Found') - - const bapIds = users.map((user) => { - return user.bapId - }) + if (!users) { + return console.log('Users Not Found') + } + const bapIds = users.map((user) => user.bapId) const baps = await bapQueries.findByIds(bapIds) - if (!baps) return console.log('BAP Not Found') - - let usersWithBapAndAttendance = users.map((user) => ({ - ...user, - bapInfo: baps.find((f) => f._id.toString() == user.bapId.toString()), - sessionAttendance: sessionAttendance.find((f) => f.userId.toString() == user._id.toString()), - })) - - usersWithBapAndAttendance.map(async function (user) { + if (!baps) { + return console.log('BAP Not Found') + } + let usersWithBapAndAttendance = users.map((user) => { + const bapInfo = baps.find((f) => f._id.toString() === user.bapId.toString()) + const sessionAttendanceInfo = sessionAttendance.find((f) => f.userId.toString() === user._id.toString()) + return { + ...user, + bapInfo, + sessionAttendanceInfo, + } + }) + usersWithBapAndAttendance.map(async (user) => { await protocolCallbacks.onUpdate({ transactionId: crypto.randomUUID(), messageId: crypto.randomUUID(), bapId: user.bapInfo.bapId, bapUri: user.bapInfo.bapUri, - status: user.sessionAttendance.statusText, + status: user.sessionAttendanceInfo.statusText, sessionId: sessionId.toString(), - fulfillmentId: user.sessionAttendance.fulfillmentId.toString(), - orderId: user.sessionAttendance.orderId.toString(), + fulfillmentId: user.sessionAttendanceInfo.fulfillmentId.toString(), + orderId: user.sessionAttendanceInfo.orderId.toString(), }) }) } catch (err) {