Skip to content

Commit

Permalink
Merge pull request #1139 from dm3-org/Rc-1.6.0-fix
Browse files Browse the repository at this point in the history
add createOrUpdateConversation db method
  • Loading branch information
AlexNi245 authored Aug 8, 2024
2 parents 95c682c + 5fbe691 commit 9b567c2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PrismaClient } from '@prisma/client';
import { createOrUpdateConversation } from './utils/createOrUpdateConversation';
import { getOrCreateAccount } from './utils/getOrCreateAccount';
import { getOrCreateConversation } from './utils/getOrCreateConversation';
export const addConversation =
(db: PrismaClient) =>
async (
Expand All @@ -10,7 +10,7 @@ export const addConversation =
) => {
try {
const account = await getOrCreateAccount(db, ensName);
await getOrCreateConversation(
await createOrUpdateConversation(
db,
account.id,
contactName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { PrismaClient } from '@prisma/client';

export const createOrUpdateConversation = async (
db: PrismaClient,
accountId: string,
encryptedContactName: string,
encryptedProfileLocation: string,
) => {
//Check if conversation already exists
const conversation = await db.conversation.findFirst({
where: {
accountId,
encryptedContactName,
},
});
if (conversation) {
//If a conversation already exist. Update the encryptedProfileLocation.
//At the moemnt this is the only updatable field
await db.conversation.update({
where: {
id: conversation.id,
},
data: {
encryptedProfileLocation,
},
});

//If conversation exists, return it
return conversation;
}
//If conversation does not exist, create it
return await db.conversation.create({
data: {
accountId,
encryptedProfileLocation,
encryptedContactName,
//Internal field to order conversations properly
//Will set whenever a conversation is created or a message is added
updatedAt: new Date(),
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export const getOrCreateConversation = async (
db: PrismaClient,
accountId: string,
encryptedContactName: string,
encryptedProfileLocation: string = '',
) => {
//Check if conversation already exists
const conversation = await db.conversation.findFirst({
Expand All @@ -21,7 +20,6 @@ export const getOrCreateConversation = async (
return await db.conversation.create({
data: {
accountId,
encryptedProfileLocation,
encryptedContactName,
//Internal field to order conversations properly
//Will set whenever a conversation is created or a message is added
Expand Down
46 changes: 46 additions & 0 deletions packages/backend/src/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,52 @@ describe('Storage', () => {
expect(body[0].encryptedProfileLocation).toEqual('123');
expect(body.length).toBe(1);
});
it('can update existing conversation with encryptedProfileLocation', async () => {
const aliceId = 'alice.eth';

const { status } = await request(app)
.post(`/new/bob.eth/addConversation`)
.set({
authorization: 'Bearer ' + token,
})
.send({
encryptedContactName: aliceId,
encryptedProfileLocation: '123',
});
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(aliceId);
expect(body[0].encryptedProfileLocation).toEqual('123');
expect(body.length).toBe(1);

const { status: updateStatus } = await request(app)
.post(`/new/bob.eth/addConversation`)
.set({
authorization: 'Bearer ' + token,
})
.send({
encryptedContactName: aliceId,
encryptedProfileLocation: '123456',
});
expect(updateStatus).toBe(200);

const { body: updatedBody } = await request(app)
.get(`/new/bob.eth/getConversations`)
.set({
authorization: 'Bearer ' + token,
});

expect(updatedBody[0].contact).toEqual(aliceId);
expect(updatedBody[0].encryptedProfileLocation).toEqual('123456');
});
it('handle duplicates add conversation', async () => {
const aliceId = 'alice.eth';
const ronId = 'ron.eth';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ export const handleMessagesFromWebSocket = async (
await resolveTLDtoAlias(decryptedEnvelop.message.metadata.from),
);

decryptedEnvelop.message.metadata.from = contact;
//TODO use TLD name
await addConversation(contact);
await addConversation(decryptedEnvelop.message.metadata.from);

const messageState =
selectedContact?.contactDetails.account.ensName === contact
Expand Down
2 changes: 1 addition & 1 deletion packages/messenger-widget/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '1.5.0';
export const version = '1.6.0';

0 comments on commit 9b567c2

Please sign in to comment.