Skip to content

Commit

Permalink
Merge pull request #18 from azuya/main
Browse files Browse the repository at this point in the history
Update schema.prisma add reportingTokenInfo field
  • Loading branch information
nizarfadlan authored Oct 6, 2024
2 parents bca04d0 + 8f21bc7 commit 22cdfea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ model Message {
verifiedBizName String? @db.VarChar(128)
eventResponses Json?
pinInChat Json?
reportingTokenInfo Json?
@@unique([sessionId, remoteJid, id], map: "unique_message_key_per_session_id")
@@index([sessionId])
Expand Down
77 changes: 45 additions & 32 deletions src/whatsapp/store/handlers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,51 @@ export default function chatHandler(sessionId: string, event: BaileysEventEmitte
};

const update: BaileysEventHandler<"chats.update"> = async (updates) => {
for (const update of updates) {
try {
const data = transformPrisma(update) as MakeTransformedPrisma<Chat>;
await model.update({
select: { pkId: true },
data: {
...data,
unreadCount:
typeof data.unreadCount === "number"
? data.unreadCount > 0
? { increment: data.unreadCount }
: { set: data.unreadCount }
: undefined,
},
where: { sessionId_id: { id: update.id!, sessionId } },
});
emitEvent("chats.update", sessionId, { chats: data });
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
return logger.info({ update }, "Got update for non existent chat");
}
emitEvent(
"chats.update",
sessionId,
undefined,
"error",
`An error occured during chat update: ${e.message}`,
);
logger.error(e, "An error occured during chat update");
}
}
};
for (const update of updates) {
try {
const data = transformPrisma(update) as MakeTransformedPrisma<Chat>;
// Cek apakah chat sudah ada sebelum mencoba mengupdate note: terkadang chat tidak seluruhnya tercatat di database @todo: cek ulang?
const existingChat = await model.findUnique({
where: { sessionId_id: { id: update.id!, sessionId } },
});

if (!existingChat) {
logger.info({ update }, "Chat not found, skipping update");
continue;
}

await model.update({
select: { pkId: true },
data: {
...data,
unreadCount:
typeof data.unreadCount === "number"
? data.unreadCount > 0
? { increment: data.unreadCount }
: { set: data.unreadCount }
: undefined,
},
where: { sessionId_id: { id: update.id!, sessionId } },
});
emitEvent("chats.update", sessionId, { chats: data });
} catch (e) {
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
return logger.info({ update }, "Got update for non existent chat");
}

// Emit event error
emitEvent(
"chats.update",
sessionId,
undefined,
"error",
`An error occurred during chat update: ${e.message}`,
);
logger.error(e, "An error occurred during chat update");
}
}
};


const del: BaileysEventHandler<"chats.delete"> = async (ids) => {
try {
Expand Down

0 comments on commit 22cdfea

Please sign in to comment.