diff --git a/packages/backend/src/persistence/storage/postgres/addMessageBatch.ts b/packages/backend/src/persistence/storage/postgres/addMessageBatch.ts index 608383fae..7db15fc43 100644 --- a/packages/backend/src/persistence/storage/postgres/addMessageBatch.ts +++ b/packages/backend/src/persistence/storage/postgres/addMessageBatch.ts @@ -19,8 +19,15 @@ export const addMessageBatch = account.id, encryptedContactName, ); + + const uniqueMessageBatch = messageBatch.filter( + (message, index, self) => + index === + self.findIndex((m) => m.messageId === message.messageId), + ); + //store each message in the db - const createMessagePromises = messageBatch.map( + const createMessagePromises = uniqueMessageBatch.map( ({ messageId, createdAt, diff --git a/packages/backend/src/storage.test.ts b/packages/backend/src/storage.test.ts index f8beaf672..42c128aec 100644 --- a/packages/backend/src/storage.test.ts +++ b/packages/backend/src/storage.test.ts @@ -1146,6 +1146,75 @@ describe('Storage', () => { expect(body[0].contact).toEqual(sha256(receiver.account.ensName)); expect(body.length).toBe(1); + const { status: getMessagesStatus, body: messages } = await request( + app, + ) + .get( + `/new/bob.eth/getMessages/${sha256( + receiver.account.ensName, + )}`, + ) + .set({ + authorization: 'Bearer ' + token, + }) + .send(); + + expect(getMessagesStatus).toBe(200); + expect(messages.length).toBe(2); + expect( + JSON.parse(JSON.parse(messages[0]).encryptedEnvelopContainer), + ).toStrictEqual(envelop); + }); + it('if batch contains duplicates it only creates the message once', async () => { + const messageFactory = MockMessageFactory( + sender, + receiver, + deliveryService, + ); + const envelop = await messageFactory.createEncryptedEnvelop( + 'Hello1', + ); + const { status } = await request(app) + .post(`/new/bob.eth/addMessageBatch`) + .set({ + authorization: 'Bearer ' + token, + }) + .send({ + encryptedContactName: sha256(receiver.account.ensName), + messageBatch: [ + { + encryptedEnvelopContainer: JSON.stringify(envelop), + messageId: '123', + createdAt: 1, + isHalted: false, + }, + { + encryptedEnvelopContainer: JSON.stringify(envelop), + messageId: '123', + createdAt: 1, + isHalted: false, + }, + { + encryptedEnvelopContainer: JSON.stringify(envelop), + messageId: '456', + createdAt: 2, + isHalted: false, + }, + ], + }); + expect(status).toBe(200); + + const { body } = await request(app) + .get(`/new/bob.eth/getConversations`) + .set({ + authorization: 'Bearer ' + token, + }) + .send(); + + expect(status).toBe(200); + expect(body[0].contact).toEqual(sha256(receiver.account.ensName)); + expect(body.length).toBe(1); + const { status: getMessagesStatus, body: messages } = await request( app, )