Skip to content

Commit

Permalink
Merge branch 'develop' into usePrismaAccountsInBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
malteish committed Aug 9, 2024
2 parents 94f93ae + b1aac28 commit 242e9b1
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ app.use(bodyParser.json());
app.use(logRequest);

app.get('/hello', (req, res) => {
return res.send('Hello DM3');
return res.status(200).send('Hello DM3');
});
app.use('/profile', Profile(db, web3Provider, serverSecret));
app.use('/storage', Storage(db, web3Provider, serverSecret));
Expand Down
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 @@ -142,6 +142,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
12 changes: 6 additions & 6 deletions packages/backend/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default (
message.encryptedEnvelopContainer,
})),
);
return res.send();
return res.sendStatus(200);
} catch (e) {
next(e);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ export default (
],
);
if (success) {
return res.send();
return res.sendStatus(200);
}
res.status(400).send('unable to add message');
} catch (e) {
Expand Down Expand Up @@ -141,7 +141,7 @@ export default (
isHalted: message.isHalted,
})),
);
return res.send();
return res.sendStatus(200);
} catch (e) {
return res.status(400).send('unable to add message batch');
}
Expand Down Expand Up @@ -220,7 +220,7 @@ export default (
_encryptedProfileLocation,
);
if (success) {
return res.send();
return res.sendStatus(200);
}
res.status(400).send('unable to add conversation');
} catch (e) {
Expand Down Expand Up @@ -298,7 +298,7 @@ export default (
);

if (success) {
return res.send();
return res.sendStatus(200);
}
res.status(400).send('unable to clear halted message');
} catch (err) {
Expand All @@ -324,7 +324,7 @@ export default (
encryptedContactName,
hide,
);
return res.send();
return res.sendStatus(200);
} catch (e) {
return res
.status(400)
Expand Down
2 changes: 1 addition & 1 deletion packages/delivery-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ global.logger = winston.createLogger({
app.use(logRequest);

app.get('/hello', (req, res) => {
return res.send('Hello DM3');
return res.status(200).send('Hello DM3');
});

//Auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function handleSubmitMessage(

try {
await messageProcessor.processEnvelop(envelop);
res.send(200);
res.sendStatus(200);
} catch (error) {
console.error('handle submit message error');
console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/server-side/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const Auth = (
);

if (!schemaIsValid) {
return res.send(400);
return res.sendStatus(400);
}

const challenge = await createChallenge(
Expand Down Expand Up @@ -85,7 +85,7 @@ export const Auth = (
const schemaIsValid = paramsAreValid && bodyIsValid;

if (!schemaIsValid) {
return res.send(400);
return res.sendStatus(400);
}

const jwt = await createNewSessionToken(
Expand Down
12 changes: 6 additions & 6 deletions packages/lib/server-side/src/authorize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Utils', () => {

//Mock request auth protected
router.get('/:address', (req, res) => {
return res.send(200);
return res.sendStatus(200);
});

const { status, body } = await request(app)
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('Utils', () => {

//Mock request auth protected
router.get('/:address', (req, res) => {
return res.send(200);
return res.sendStatus(200);
});

const { status, body } = await request(app)
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Utils', () => {

//Mock request auth protected
router.get('/:address', (req, res) => {
return res.send(200);
return res.sendStatus(200);
});

app.locals.web3Provider = {
Expand Down Expand Up @@ -205,7 +205,7 @@ describe('Utils', () => {

//Mock request auth protected
router.get('/:address', (req, res) => {
return res.send(200);
return res.sendStatus(200);
});

const { status, body } = await request(app)
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('Utils', () => {

//Mock request auth protected
router.get('/:address', (req, res) => {
return res.send(200);
return res.sendStatus(200);
});

const { status, body } = await request(app)
Expand Down Expand Up @@ -363,7 +363,7 @@ describe('Utils', () => {

//Mock request auth protected
router.get('/:address', (req, res) => {
return res.send(200);
return res.sendStatus(200);
});

const { status, body } = await request(app)
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
4 changes: 2 additions & 2 deletions packages/offchain-resolver/src/http/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ export function profile(web3Provider: ethers.providers.BaseProvider) {
));

if (!hasAddressProfile) {
return res.send(404);
return res.sendStatus(404);
}
const profileContainer =
await req.app.locals.db.getProfileContainerByAddress(address);
if (!profileContainer) {
return res.send(404);
return res.sendStatus(404);
}

return res.status(200).send(profileContainer.profile);
Expand Down

0 comments on commit 242e9b1

Please sign in to comment.