From 250acf6c1d0d63b5bf1beb9a625f9e57b2d930ed Mon Sep 17 00:00:00 2001 From: Miroslav Kovar Date: Mon, 11 Apr 2022 09:32:58 +0200 Subject: [PATCH] Optimize message status updates (#159) * Optimize message status updates * Remove unused functions Signed-off-by: Miroslav Kovar --- .../20220407134403-unique-uid-constrain.js | 53 ++++++++ ...220407134403-unique-uid-constrain-down.sql | 1 + ...20220407134403-unique-uid-constrain-up.sql | 1 + .../src/messaging/client2agency/msgs-agent.js | 6 +- .../src/service/entities/agent/agent.js | 51 ++------ vcxagency-node/src/service/storage/storage.js | 114 +----------------- .../test/unit/messaging/aconn-msgs.spec.js | 18 ++- .../test/unit/messaging/agent-msgs.spec.js | 43 +++---- .../test/unit/storage/pgstorage-links.spec.js | 38 ------ .../pgstorage-messages-retrieval.spec.js | 60 +++++---- .../storage/pgstorage-messages-update.spec.js | 17 +-- 11 files changed, 151 insertions(+), 251 deletions(-) create mode 100644 dbutils/migrations_scheme_application/20220407134403-unique-uid-constrain.js create mode 100644 dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-down.sql create mode 100644 dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-up.sql diff --git a/dbutils/migrations_scheme_application/20220407134403-unique-uid-constrain.js b/dbutils/migrations_scheme_application/20220407134403-unique-uid-constrain.js new file mode 100644 index 00000000..82434628 --- /dev/null +++ b/dbutils/migrations_scheme_application/20220407134403-unique-uid-constrain.js @@ -0,0 +1,53 @@ +'use strict'; + +var dbm; +var type; +var seed; +var fs = require('fs'); +var path = require('path'); +var Promise; + +/** + * We receive the dbmigrate dependency from dbmigrate initially. + * This enables us to not have to rely on NODE_PATH. + */ +exports.setup = function(options, seedLink) { + dbm = options.dbmigrate; + type = dbm.dataType; + seed = seedLink; + Promise = options.Promise; +}; + +exports.up = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220407134403-unique-uid-constrain-up.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports.down = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220407134403-unique-uid-constrain-down.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports._meta = { + "version": 1 +}; diff --git a/dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-down.sql b/dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-down.sql new file mode 100644 index 00000000..44f074ea --- /dev/null +++ b/dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-down.sql @@ -0,0 +1 @@ +/* Replace with your SQL commands */ \ No newline at end of file diff --git a/dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-up.sql b/dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-up.sql new file mode 100644 index 00000000..23209057 --- /dev/null +++ b/dbutils/migrations_scheme_application/sqls/20220407134403-unique-uid-constrain-up.sql @@ -0,0 +1 @@ +ALTER TABLE messages ADD CONSTRAINT unique_uid UNIQUE (uid) diff --git a/vcxagency-client/src/messaging/client2agency/msgs-agent.js b/vcxagency-client/src/messaging/client2agency/msgs-agent.js index 855c7435..725d4044 100644 --- a/vcxagency-client/src/messaging/client2agency/msgs-agent.js +++ b/vcxagency-client/src/messaging/client2agency/msgs-agent.js @@ -75,11 +75,11 @@ function buildMsgVcxV2UpdateMsgStatusByConns (statusCode, uidsByConns) { return msg } -function buildMsgVcxV2MsgStatusUpdatedByConns (failedUidsByConns, updatedUidsByConns) { +function buildMsgVcxV2MsgStatusUpdatedByConns () { const msg = { '@type': MSGTYPE_MSG_STATUS_UPDATED_BY_CONNS, - failed: failedUidsByConns, // [{"pairwiseDID":"Fp4eVWcjyRawjNWgnJmJWD","uids":["aBcDeF1234"]}]} - updatedUidsByConns // example: [{"pairwiseDID":"Fp4eVWcjyRawjNWgnJmJWD","uids":["b7vh36XiTe"]}]} + failed: [], + updatedUidsByConns: [] } return msg } diff --git a/vcxagency-node/src/service/entities/agent/agent.js b/vcxagency-node/src/service/entities/agent/agent.js index 9f17bb09..f235e357 100644 --- a/vcxagency-node/src/service/entities/agent/agent.js +++ b/vcxagency-node/src/service/entities/agent/agent.js @@ -225,48 +225,23 @@ async function buildAgentAO (entityRecord, serviceWallets, serviceStorage, route return buildMsgVcxV2MsgsByConns(msgsByConns) } - async function convertToUidsByPwDids (uidsByAconnDids, ignoreNotFound = false) { - const uidsByPwDids = [] - for (const rec of uidsByAconnDids) { - const { agentConnDid, uids } = rec - const userPwDid = await serviceStorage.aconnDidToPwDid(agentDid, agentConnDid) - if (userPwDid) { - uidsByPwDids.push({ pairwiseDID: userPwDid, uids }) - } else if (!ignoreNotFound) { - throw Error(`AgentConnectionDid ${agentConnDid} could not be mapped to any userPwDid.`) + // "failed":[],"updatedUidsByConns":[{"pairwiseDID":"Fp4eVWcjyRawjNWgnJmJWD","uids":["b7vh36XiTe"]}]} + async function _handleUpdateMsgsStatusByConns (statusCode, uidsByConns) { + function getUidsFromUidsByConns (uidsByConns) { + const uids = [] + for (const uidsByConn of uidsByConns) { + uids.push(...uidsByConn.uids) } + return uids } - return uidsByPwDids - } - /** - * Takes list of "uids by pw dids", example: [{"pairwiseDID":"Fp4eVWcjyRawjNWgnJmJWD","uids":["b7vh36XiTe"]}]} - * and converts it to "uids by agent connection dids": [{"agentConnDid":"abcd1234565","uids":["b7vh36XiTe"]}]} - * So that each userPairwiseDid is converted to agentConnectionDid - * @param {array} uidsByPwDids - Message data - * @param {boolean} ignoreNotFound - Message data - */ - async function convertToUidsByAgentConnDids (uidsByPwDids, ignoreNotFound = false) { - const uidsByAconnDids = [] - for (const rec of uidsByPwDids) { - const { pairwiseDID, uids } = rec - const agentConnDid = await serviceStorage.pwDidToAconnDid(agentDid, pairwiseDID) - if (agentConnDid) { - uidsByAconnDids.push({ agentConnDid, uids }) - } else if (!ignoreNotFound) { - throw Error(`UserPwDid ${pairwiseDID} could not be mapped to agenConnectionDid.`) - } + if (!uidsByConns || !Array.isArray(uidsByConns)) { + logger.warn(`Agent ${agentDid} attempting to update status codes, but uidsByConns ${uidsByConns} is empty or not an array`) + return } - return uidsByAconnDids - } - - // "failed":[],"updatedUidsByConns":[{"pairwiseDID":"Fp4eVWcjyRawjNWgnJmJWD","uids":["b7vh36XiTe"]}]} - async function _handleUpdateMsgsStatusByConns (statusCode, uidsByConns) { - const uidsByAgentConnDids = await convertToUidsByAgentConnDids(uidsByConns, true) - const { failed, updated } = await serviceStorage.updateStatusCodes(agentDid, uidsByAgentConnDids, statusCode) - const updatedUidsByConns = await convertToUidsByPwDids(updated) - const failedUidsByConns = await convertToUidsByPwDids(failed) - return buildMsgVcxV2MsgStatusUpdatedByConns(failedUidsByConns, updatedUidsByConns) + const uids = getUidsFromUidsByConns(uidsByConns) + await serviceStorage.updateStatusCodeAgentConnection(agentDid, uids, statusCode) + return buildMsgVcxV2MsgStatusUpdatedByConns() } async function _handleSignUpMsg (_msgObject) { diff --git a/vcxagency-node/src/service/storage/storage.js b/vcxagency-node/src/service/storage/storage.js index b4c7eb9b..469c11b6 100644 --- a/vcxagency-node/src/service/storage/storage.js +++ b/vcxagency-node/src/service/storage/storage.js @@ -188,54 +188,20 @@ async function createDataStorage (appStorageConfig) { /** * Updates statusCodes for messages on particular agent and its particular connection. * @param {string} agentDid - DID of the agent. - * @param {string} agentConnDid - DID of the agent connection. Must belong to agent. * @param {array} uids - list of message UIDs for whom new status should be set. * @param {string} newStatusCode - New status code to be set for matched messages. */ - async function updateStatusCodeAgentConnection (agentDid, agentConnDid, uids, newStatusCode) { - if (!agentDid || !agentConnDid) { - throw Error('AgentDid or AgentConnDid was not specified.') + async function updateStatusCodeAgentConnection (agentDid, uids, newStatusCode) { + if (!agentDid) { + throw Error(`Failed to update status codes for messages ${uids}: AgentDid was not specified.`) } if (uids.length === 0) { - return { failedUids: [], updatedUids: [] } + return } - // todo: before we run actual update, we should port: see dummy's check_if_message_status_can_be_updated - // see: https://github.com/hyperledger/indy-sdk/blob/d3057f1e21f01768104ca129de63a15d1b5e302e/vcx/dummy-cloud-agent/src/actors/agent_connection.rs - // But Dummy Cloud Agency throws error if a single message in the set cannot be updated which seems like - // a strange behaviour. Rather the uid should rather be just included in failedUids portion of response? - // dummy cloud agency always return empty array for failed uids const values = [newStatusCode, agentDid, uids] const query = 'UPDATE messages SET status_code = ? WHERE agent_did = ? AND uid IN(?)' if (process.env.EXPLAIN_QUERIES === 'true') { explainQuery(query, values) } await timeOperation(queryDb, { query, values, opName: 'updateStatusCodeAgentConnection' }, query, values) - const updatedUids = uids // difficult to find out which UIDs were successfully updated with mysql, - const failedUids = [] // unless we update one by one. With pgsql doable with RETURNING clause. - return { failedUids, updatedUids } - } - - /** - * Updates statusCodes for messages on particular agent across many connections. - * @param {string} agentDid - DID of agent message belongs to - * @param {string} statusCode - New status code to be set for matched messages. - * @param {array} uidsByAgentConnDids - example: [{"aconnDid":"6FRuB95abcmzz1nURoHyWE","uids":["Br4CoNP4TU"]}, ...] - */ - async function updateStatusCodes (agentDid, uidsByAgentConnDids, statusCode) { - const failed = [] - const updated = [] - for (const uidsByConn of uidsByAgentConnDids) { - const { agentConnDid, uids } = uidsByConn - const { - failedUids, - updatedUids - } = await updateStatusCodeAgentConnection(agentDid, agentConnDid, uids, statusCode) - if (failedUids.length > 0) { - failed.push({ agentConnDid, uids: failedUids }) - } - if (updatedUids.length > 0) { - updated.push({ agentConnDid, uids: updatedUids }) - } - } - return { failed, updated } } // ---- ---- ---- ---- ---- ---- Entity records read/write @@ -320,32 +286,6 @@ async function createDataStorage (appStorageConfig) { ) } - /** - * For Agent's connection, converts "User Pairwise Did" into "Agent Connection Did" - * @param {string} agentDid - DID of an Agent owning connection - * @param {string} pwDid - "User Pairwise DID" associated with the connection - */ - async function pwDidToAconnDid (agentDid, pwDid) { - const res = await aconnLinkPairsByPwDids(agentDid, [pwDid]) - if (res.length === 0) { - return undefined - } - return res[0].agentConnDid - } - - /** - * For Agent's connection, converts "Agent Connection Did" into "User Pairwise Did" - * @param {string} agentDid - DID of an Agent owning connection - * @param {string} aconnDid - "User Pairwise DID" associated with the connection - */ - async function aconnDidToPwDid (agentDid, aconnDid) { - const res = await aconnLinkPairsByAconnDids(agentDid, [aconnDid]) - if (res.length === 0) { - return undefined - } - return res[0].userPwDid - } - /** * Returns list of tuples ( "Agent Connection Did", "User pairwise Did"), each representing one connection. * @param {string} agentDid - DID of an Agent owning the connection. @@ -382,46 +322,6 @@ async function createDataStorage (appStorageConfig) { return pairs } - /** - * Maps updateByConn by pwDid to updateByConn by agentConnDid. - * Example: maps [{"pairwiseDID":"12346543AAAAEFGHRoHyWE","uids":["Br4CoNP4TU"]}] to [{"agentConnDid":"6FRuB95abcmzz1nURoHyWE","uids":["Br4CoNP4TU"]}] - * @param {string} agentDid - DID of an Agent owning connection - * @param {array} uidsByUserPwDids - updateByConn by pwDid - * If one of the agent connection DIDs cannot be mapped into pairwiseDID, the update record will be omitted in response - */ - async function convertIntoStorageRequest (agentDid, uidsByUserPwDids) { - const res = [] - for (const uidsByUserPwDid of uidsByUserPwDids) { - const { pairwiseDID, uids } = uidsByUserPwDid - const mapped = await aconnLinkPairsByPwDids(agentDid, [pairwiseDID]) - if (mapped.length === 1) { - const { agentConnDid } = mapped[0] - res.push({ agentConnDid, uids }) - } - } - return res - } - - /** - * Maps updateByConn by agentConnDid to updateByConn by pwDid. - * Example: maps [{"agentConnDid":"6FRuB95abcmzz1nURoHyWE","uids":["Br4CoNP4TU"]}] to [{"pairwiseDID":"12346543AAAAEFGHRoHyWE","uids":["Br4CoNP4TU"]}] - * @param {string} agentDid - DID of an Agent owning connection - * @param {array} uidsByUserAgentConnDids - updateByConn by agent connection DIDs - * If one of the agent connection DIDs cannot be mapped into pairwiseDID, the update record will be omitted in response - */ - async function convertIntoUserUpdateResponse (agentDid, uidsByUserAgentConnDids) { - const res = [] - for (const uidsByAgentConnDid of uidsByUserAgentConnDids) { - const { agentConnDid, uids } = uidsByAgentConnDid - const mapped = await aconnLinkPairsByAconnDids(agentDid, [agentConnDid]) - if (mapped.length === 1) { - const { userPwDid: pairwiseDID } = mapped[0] - res.push({ pairwiseDID, uids }) - } - } - return res - } - function cleanUp () { pool.end() } @@ -433,7 +333,7 @@ async function createDataStorage (appStorageConfig) { saveEntityRecord, // messaging - updateStatusCodes, + updateStatusCodeAgentConnection, storeMessage, loadMessages, @@ -446,10 +346,6 @@ async function createDataStorage (appStorageConfig) { linkAgentToItsConnection, aconnLinkPairsByPwDids, aconnLinkPairsByAconnDids, - convertIntoStorageRequest, - convertIntoUserUpdateResponse, - pwDidToAconnDid, - aconnDidToPwDid, setHasNewMessage, getHasNewMessage, diff --git a/vcxagency-node/test/unit/messaging/aconn-msgs.spec.js b/vcxagency-node/test/unit/messaging/aconn-msgs.spec.js index 8c17eb1d..36c2cfc2 100644 --- a/vcxagency-node/test/unit/messaging/aconn-msgs.spec.js +++ b/vcxagency-node/test/unit/messaging/aconn-msgs.spec.js @@ -58,6 +58,18 @@ let sendToAgency let tmpDbData let tmpDbWallet +let msg1Id +let msg2Id +let msg3Id +let msg4Id + +function regenerateUuids () { + msg1Id = uuid.v4() + msg2Id = uuid.v4() + msg3Id = uuid.v4() + msg4Id = uuid.v4() +} + beforeAll(async () => { try { jest.setTimeout(1000 * 120) @@ -94,6 +106,7 @@ afterAll(async () => { }) beforeEach(async () => { + regenerateUuids() { agencyUserWalletKey = await indyGenerateWalletKey() agencyUserWalletName = `unit-test-${uuid.v4()}` @@ -132,11 +145,6 @@ let aconnDid let aconnVerkey let aconnUserPwVkey -const msg1Id = uuid.v4() -const msg2Id = uuid.v4() -const msg3Id = uuid.v4() -const msg4Id = uuid.v4() - let agent1Did let agent1Verkey diff --git a/vcxagency-node/test/unit/messaging/agent-msgs.spec.js b/vcxagency-node/test/unit/messaging/agent-msgs.spec.js index 952bd569..c0f4552a 100644 --- a/vcxagency-node/test/unit/messaging/agent-msgs.spec.js +++ b/vcxagency-node/test/unit/messaging/agent-msgs.spec.js @@ -70,6 +70,22 @@ let sendToAgency let tmpDbData let tmpDbWallet +let msg1Id +let msg2Id +let msg3Id +let msg4Id +let msg5Id +let msg6Id + +function regenerateUuids () { + msg1Id = uuid.v4() + msg2Id = uuid.v4() + msg3Id = uuid.v4() + msg4Id = uuid.v4() + msg5Id = uuid.v4() + msg6Id = uuid.v4() +} + beforeAll(async () => { try { jest.setTimeout(1000 * 120) @@ -106,6 +122,7 @@ afterAll(async () => { }) beforeEach(async () => { + regenerateUuids() { agencyUserWalletKey = await indyGenerateWalletKey() agencyUserWalletName = `unit-test-${uuid.v4()}` @@ -178,13 +195,6 @@ let aconn1UserPwDid let aconn2UserPwDid let aconn3UserPwDid -const msg1Id = uuid.v4() -const msg2Id = uuid.v4() -const msg3Id = uuid.v4() -const msg4Id = uuid.v4() -const msg5Id = uuid.v4() -const msg6Id = uuid.v4() - let agent1Did let agent1Verkey @@ -350,25 +360,12 @@ describe('onboarding', () => { it('should update statusCodes of messages', async () => { // act const uidsByConn = [ - { pairwiseDID: aconn1UserPwDid, uids: [msg1Id, msg2Id] }, - { pairwiseDID: aconn2UserPwDid, uids: [msg6Id] } + { pairwiseDID: 'not-taken-in-consideration', uids: [msg1Id, msg2Id] }, + { pairwiseDID: 'not-taken-in-consideration', uids: [msg6Id] } ] const updateRes = await vcxFlowUpdateMsgsFromAgent(agencyUserWh, sendToAgency, agent1Did, agent1Verkey, agencyUserVerkey, uidsByConn, 'MS-106') expect(updateRes['@type']).toBe('did:sov:123456789abcdefghi1234;spec/pairwise/1.0/MSG_STATUS_UPDATED_BY_CONNS') - expect(updateRes.updatedUidsByConns.length).toBe(2) - const updated1 = updateRes.updatedUidsByConns.find(update => update.pairwiseDID === aconn1UserPwDid) - const updated2 = updateRes.updatedUidsByConns.find(update => update.pairwiseDID === aconn2UserPwDid) - - expect(updated1).toBeDefined() - expect(updated1.uids.length).toBe(2) - expect(updated1.uids.includes(msg1Id)).toBeTruthy() - expect(updated1.uids.includes(msg2Id)).toBeTruthy() - - expect(updated2).toBeDefined() - expect(updated2.uids.length).toBe(1) - expect(updated2.uids.includes(msg6Id)).toBeTruthy() - let msgReply = await vcxFlowGetMsgsFromAgent(agencyUserWh, sendToAgency, agent1Did, agent1Verkey, agencyUserVerkey, [aconn1UserPwDid, aconn2UserPwDid], [], []) let { msgsByConns } = msgReply expect(Array.isArray(msgsByConns)).toBeTruthy() @@ -414,8 +411,6 @@ describe('onboarding', () => { ] const updateRes = await vcxFlowUpdateMsgsFromAgent(agencyUserWh, sendToAgency, agent1Did, agent1Verkey, agencyUserVerkey, uidsByConn, 'MS-106') expect(updateRes['@type']).toBe('did:sov:123456789abcdefghi1234;spec/pairwise/1.0/MSG_STATUS_UPDATED_BY_CONNS') - expect(updateRes.failed.length).toBe(0) - expect(updateRes.updatedUidsByConns.length).toBe(0) }) function assertMsgsByConHasMessageWithStatus (msgsByConn, msgId, expectedStatusCode) { diff --git a/vcxagency-node/test/unit/storage/pgstorage-links.spec.js b/vcxagency-node/test/unit/storage/pgstorage-links.spec.js index 8b359b3f..f6b08780 100644 --- a/vcxagency-node/test/unit/storage/pgstorage-links.spec.js +++ b/vcxagency-node/test/unit/storage/pgstorage-links.spec.js @@ -91,42 +91,4 @@ describe('storage', () => { const res4 = await storage.aconnLinkPairsByAconnDids(A1, ['12321321312312']) expect(res4.length).toBe(0) }) - - it('should convert uidsByAconnDids to uidsByPwDids', async () => { - // arrange - const uidsByAconnDids = [{ agentConnDid: A1Conn1, uids: ['foo'] }, { agentConnDid: A1Conn2, uids: ['bar'] }] - - // act - const uidsByPwDids = await storage.convertIntoUserUpdateResponse(A1, uidsByAconnDids) - - // assert - expect(uidsByPwDids.length).toBe(2) - - const r1 = uidsByPwDids.find(r => r.pairwiseDID === A1Conn1Pw) - expect(r1).toBeDefined() - expect(r1.uids.includes('foo')).toBeTruthy() - - const r2 = uidsByPwDids.find(r => r.pairwiseDID === A1Conn2Pw) - expect(r2).toBeDefined() - expect(r2.uids.includes('bar')).toBeTruthy() - }) - - it('should convert uidsByPwDids to uidsByAconnDids', async () => { - // arrange - const uidsByAconnDids = [{ pairwiseDID: A1Conn1Pw, uids: ['foo'] }, { pairwiseDID: A1Conn2Pw, uids: ['bar'] }] - - // act - const uidsByPwDids = await storage.convertIntoStorageRequest(A1, uidsByAconnDids) - - // assert - expect(uidsByPwDids.length).toBe(2) - - const r1 = uidsByPwDids.find(r => r.agentConnDid === A1Conn1) - expect(r1).toBeDefined() - expect(r1.uids.includes('foo')).toBeTruthy() - - const r2 = uidsByPwDids.find(r => r.agentConnDid === A1Conn2) - expect(r2).toBeDefined() - expect(r2.uids.includes('bar')).toBeTruthy() - }) }) diff --git a/vcxagency-node/test/unit/storage/pgstorage-messages-retrieval.spec.js b/vcxagency-node/test/unit/storage/pgstorage-messages-retrieval.spec.js index 57898c43..eb645f4d 100644 --- a/vcxagency-node/test/unit/storage/pgstorage-messages-retrieval.spec.js +++ b/vcxagency-node/test/unit/storage/pgstorage-messages-retrieval.spec.js @@ -40,6 +40,9 @@ describe('storage', () => { const uid1 = uuid.v4() const uid2 = uuid.v4() const uid3 = uuid.v4() + const uid4 = uuid.v4() + const uid5 = uuid.v4() + const uid6 = uuid.v4() const a1Conn1Did = uuid.v4() const a1Conn2Did = uuid.v4() const a2Conn1Did = uuid.v4() @@ -51,22 +54,22 @@ describe('storage', () => { await storage.storeMessage(agentDid, a1Conn2Did, uid2, 'MS-104', { msg: 'a1hello2' }) await storage.storeMessage(agentDid, a1Conn2Did, uid3, 'MS-105', { msg: 'a1hello3' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid1, 'MS-103', { msg: 'a2hello1' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid2, 'MS-104', { msg: 'a2hello2' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid3, 'MS-105', { msg: 'a2hello3' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid4, 'MS-103', { msg: 'a2hello1' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid5, 'MS-104', { msg: 'a2hello2' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid6, 'MS-105', { msg: 'a2hello3' }) // assert const agent2Msgs = await storage.loadMessages(agentDid2, [], [], []) expect(agent2Msgs.length).toBe(3) - const msg1 = agent2Msgs.find(m => m.metadata.uid === uid1) + const msg1 = agent2Msgs.find(m => m.metadata.uid === uid4) expect(msg1.metadata.statusCode).toBe('MS-103') expect(msg1.payload.msg).toBe('a2hello1') - const msg2 = agent2Msgs.find(m => m.metadata.uid === uid2) + const msg2 = agent2Msgs.find(m => m.metadata.uid === uid5) expect(msg2.metadata.statusCode).toBe('MS-104') expect(msg2.payload.msg).toBe('a2hello2') - const msg3 = agent2Msgs.find(m => m.metadata.uid === uid3) + const msg3 = agent2Msgs.find(m => m.metadata.uid === uid6) expect(msg3.metadata.statusCode).toBe('MS-105') expect(msg3.payload.msg).toBe('a2hello3') }) @@ -76,6 +79,9 @@ describe('storage', () => { const uid2 = uuid.v4() const uid3 = uuid.v4() const uid4 = uuid.v4() + const uid5 = uuid.v4() + const uid6 = uuid.v4() + const uid7 = uuid.v4() const a1Conn1Did = uuid.v4() const a1Conn2Did = uuid.v4() const a2Conn1Did = uuid.v4() @@ -89,9 +95,9 @@ describe('storage', () => { await storage.storeMessage(agentDid, a1Conn2Did, uid3, 'MS-105', { msg: 'a1hello3' }) await storage.storeMessage(agentDid, a1Conn3Did, uid4, 'MS-105', { msg: 'a1hello4' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid1, 'MS-103', { msg: 'a2hello1' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid2, 'MS-104', { msg: 'a2hello2' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid3, 'MS-105', { msg: 'a2hello3' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid5, 'MS-103', { msg: 'a2hello1' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid6, 'MS-104', { msg: 'a2hello2' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid7, 'MS-105', { msg: 'a2hello3' }) // assert const msgObjects = await storage.loadMessages(agentDid, [a1Conn1Did, a1Conn3Did], [], []) @@ -104,6 +110,9 @@ describe('storage', () => { const uid1 = uuid.v4() const uid2 = uuid.v4() const uid3 = uuid.v4() + const uid4 = uuid.v4() + const uid5 = uuid.v4() + const uid6 = uuid.v4() const a1Conn1Did = uuid.v4() const a2Conn1Did = uuid.v4() const agentDid = uuid.v4() @@ -114,9 +123,9 @@ describe('storage', () => { await storage.storeMessage(agentDid, a1Conn1Did, uid2, 'MS-104', { msg: 'msg2' }) await storage.storeMessage(agentDid, a1Conn1Did, uid3, 'MS-105', { msg: 'msg3' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid1, 'MS-103', { msg: 'xxxxxxx1' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid2, 'MS-104', { msg: 'xxxxxxx2' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid3, 'MS-105', { msg: 'xxxxxxx3' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid4, 'MS-103', { msg: 'xxxxxxx1' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid5, 'MS-104', { msg: 'xxxxxxx2' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid6, 'MS-105', { msg: 'xxxxxxx3' }) // assert const msgObjects = await storage.loadMessages(agentDid, [], [uid1, uid3], []) @@ -131,6 +140,9 @@ describe('storage', () => { const uid1 = uuid.v4() const uid2 = uuid.v4() const uid3 = uuid.v4() + const uid4 = uuid.v4() + const uid5 = uuid.v4() + const uid6 = uuid.v4() const a1Conn1Did = uuid.v4() const a2Conn1Did = uuid.v4() const agentDid = uuid.v4() @@ -141,9 +153,9 @@ describe('storage', () => { await storage.storeMessage(agentDid, a1Conn1Did, uid2, 'MS-104', { msg: 'msg2' }) await storage.storeMessage(agentDid, a1Conn1Did, uid3, 'MS-105', { msg: 'msg3' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid1, 'MS-103', { msg: 'xxxxxxx1' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid2, 'MS-104', { msg: 'xxxxxxx2' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid3, 'MS-105', { msg: 'xxxxxxx3' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid4, 'MS-103', { msg: 'xxxxxxx1' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid5, 'MS-104', { msg: 'xxxxxxx2' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid6, 'MS-105', { msg: 'xxxxxxx3' }) // assert const msgObjects = await storage.loadMessages(agentDid, [], [], ['MS-105']) @@ -159,6 +171,12 @@ describe('storage', () => { const uid5 = uuid.v4() const uid6 = uuid.v4() const uid7 = uuid.v4() + const uid8 = uuid.v4() + const uid9 = uuid.v4() + const uid10 = uuid.v4() + const uid11 = uuid.v4() + const uid12 = uuid.v4() + const uid13 = uuid.v4() const a1Conn1Did = uuid.v4() const a1Conn2Did = uuid.v4() const a2Conn1Did = uuid.v4() @@ -178,12 +196,12 @@ describe('storage', () => { await storage.storeMessage(agentDid, a1Conn2Did, uid7, 'MS-105', { msg: 'msg6' }) // agent2 - await storage.storeMessage(agentDid2, a2Conn1Did, uid1, 'MS-103', { msg: 'xxxxxxx1' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid2, 'MS-104', { msg: 'xxxxxxx2' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid3, 'MS-105', { msg: 'xxxxxxx3' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid4, 'MS-103', { msg: 'xxxxxxx1' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid5, 'MS-104', { msg: 'xxxxxxx2' }) - await storage.storeMessage(agentDid2, a2Conn1Did, uid6, 'MS-105', { msg: 'xxxxxxx3' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid8, 'MS-103', { msg: 'xxxxxxx1' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid9, 'MS-104', { msg: 'xxxxxxx2' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid10, 'MS-105', { msg: 'xxxxxxx3' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid11, 'MS-103', { msg: 'xxxxxxx1' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid12, 'MS-104', { msg: 'xxxxxxx2' }) + await storage.storeMessage(agentDid2, a2Conn1Did, uid13, 'MS-105', { msg: 'xxxxxxx3' }) // assert const res1 = await storage.loadMessages(agentDid, [a1Conn1Did], [uid1], ['MS-103']) diff --git a/vcxagency-node/test/unit/storage/pgstorage-messages-update.spec.js b/vcxagency-node/test/unit/storage/pgstorage-messages-update.spec.js index f1b8f8c3..603680c6 100644 --- a/vcxagency-node/test/unit/storage/pgstorage-messages-update.spec.js +++ b/vcxagency-node/test/unit/storage/pgstorage-messages-update.spec.js @@ -54,17 +54,9 @@ describe('storage', () => { await storage.storeMessage(agentDid, agentConn2Did, uid4, 'MS-105', { msg: 'hello4' }) // act - const { failed, updated } = - await storage.updateStatusCodes(agentDid, [{ agentConnDid: agentConn2Did, uids: [uid2, uid4] }], 'MS-106') + await storage.updateStatusCodeAgentConnection(agentDid, [uid2, uid4], 'MS-106') // assert - expect(failed.length).toBe(0) - expect(updated.length).toBe(1) - expect(updated[0].agentConnDid).toBe(agentConn2Did) - expect(updated[0].uids.length).toBe(2) - expect(updated[0].uids.find(uid => uid === uid2)).toBeDefined() - expect(updated[0].uids.find(uid => uid === uid4)).toBeDefined() - const msgObjects = await storage.loadMessages(agentDid, agentConn2Did, [], []) expect(msgObjects.length).toBe(3) const msg2 = msgObjects.find(m => m.metadata.uid === uid2) @@ -93,11 +85,10 @@ describe('storage', () => { await storage.storeMessage(agentDid, agentConn2Did, uid4, 'MS-105', { msg: 'hello4' }) // act - const { failed, updated } = - await storage.updateStatusCodes(agentDid, [{ agentConnDid: agentConn2Did, uids: [] }], 'MS-106') + await storage.updateStatusCodeAgentConnection(agentDid, [], 'MS-106') // assert - expect(failed.length).toBe(0) - expect(updated.length).toBe(0) + const msgObjects = await storage.loadMessages(agentDid, [agentConn1Did, agentConn2Did], agentConn2Did, [], ['MS-106']) + expect(msgObjects.length).toBe(0) }) })