From 4280510274d7fd83dcb584469629f87ec7f867d6 Mon Sep 17 00:00:00 2001 From: Voktor Stolenets Date: Tue, 21 Jan 2025 23:45:10 +0200 Subject: [PATCH] Change: fix mongo atomic operation --- src/server.ts | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/server.ts b/src/server.ts index 711d7ce..3c9f75b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -68,7 +68,6 @@ const startServer = async () => { }; address = address.toLowerCase(); const secret = mapSecret.get(address); - if (!signature || !address || !secret) { return reply.status(400).send({ error: "Missing address or signature in headers", @@ -132,9 +131,11 @@ async function checkTokenExist( await request.server.mongo.db?.collection("token").updateOne( { tokenAddress }, { - tokenAddress, - like: 0, - timestamp: new Date(), + $set: { + tokenAddress, + like: 0, + timestamp: new Date(), + }, }, { upsert: true }, ); @@ -202,10 +203,12 @@ async function addOrUpdateUser(request: FastifyRequest, reply: FastifyReply) { await request.server.mongo.db?.collection("user").updateOne( { address }, { - address, - userName, - image, - timestamp: new Date(), + $set: { + address, + userName, + image, + timestamp: new Date(), + }, }, { upsert: true }, ); @@ -244,9 +247,11 @@ async function addOrDeleteLike(request: FastifyRequest, reply: FastifyReply) { .updateOne( { address, tokenAddress }, { - address, - tokenAddress, - timestamp: new Date(), + $set: { + address, + tokenAddress, + timestamp: new Date(), + }, }, { upsert: true }, ); @@ -458,8 +463,10 @@ async function addfollow(request: FastifyRequest, reply: FastifyReply) { await request.server.mongo.db?.collection("followers").updateOne( { address, userAddress }, { - address, - userAddress, + $set: { + address, + userAddress, + }, }, { upsert: true }, );