feat: sendCheckingPresence debug #3349

Merged
robert merged 2 commits from sendCheckPresenceDebug into master 2025-01-10 09:13:59 +00:00
1 changed files with 39 additions and 30 deletions

View File

@ -27,38 +27,47 @@ module.exports = Self => {
});
Self.sendCheckingPresence = async(ctx, recipientId, message) => {
if (!recipientId) return false;
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const sender = await models.VnUser.findById(userId, {fields: ['id']});
const recipient = await models.VnUser.findById(recipientId, null);
// Prevent sending messages to yourself
if (recipientId == userId) return false;
if (!recipient)
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
if (!isProduction())
message = `[Test:Environment to user ${userId}] ` + message;
const chat = await models.Chat.create({
senderFk: sender.id,
recipient: `@${recipient.name}`,
dated: Date.vnNew(),
checkUserStatus: 1,
message: message,
status: 'sending',
attempts: 0
});
try {
await Self.sendCheckingUserStatus(chat);
await Self.updateChat(chat, 'sent');
} catch (error) {
await Self.updateChat(chat, 'error', error);
}
const models = Self.app.models;
const sender = await models.VnUser.findById(userId, {fields: ['id']});
const error = `Could not send message from user ${userId}`;
return true;
if (!recipientId) throw new Error(error);
const recipient = await models.VnUser.findById(recipientId, null);
if (!recipient)
throw new Error(error);
// Prevent sending messages to yourself
if (recipientId == userId) return false;
if (!isProduction())
message = `[Test:Environment to user ${userId}] ` + message;
const chat = await models.Chat.create({
senderFk: sender.id,
recipient: `@${recipient.name}`,
dated: Date.vnNew(),
checkUserStatus: 1,
message: message,
status: 'sending',
attempts: 0
});
try {
await Self.sendCheckingUserStatus(chat);
await Self.updateChat(chat, 'sent');
} catch (error) {
await Self.updateChat(chat, 'error', error);
}
return true;
} catch (e) {
await Self.rawSql(`
INSERT INTO util.debug (variable, value)
VALUES ('sendCheckingPresence_error', ?)
`, [`User: ${userId}, recipient: ${recipientId}, message: ${message}, error: ${e}`]);
throw e;
}
};
};