Skip to content

Commit

Permalink
rewrite for more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Nov 10, 2023
1 parent 7b7e806 commit 354b03b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions adapters/otp/default/defaultOTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class DefaultOTP extends OneTimePasswordAdapter_1.default {
await NotificationManager.sendMessageToDeliveryManager("error", `Failed OPT password generate for ${login}, please contact with him`);
throw `otp generation error`;
}
let mainLoginField = await Settings.get("LOGIN_FIELD");
let mainLoginField = await Settings.get("LOGIN_FIELD") ?? 'phone';
if (NotificationManager.isChannelExist(mainLoginField === "phone" ? "sms" : mainLoginField)) {
try {
await NotificationManager.sendMessageToUser("info", `Your code is ${otp.password}`, login);
await NotificationManager.sendMessageToUser("info", `Your code is ${otp.password}`, { phone: { code: "", number: login } });
}
catch (error) {
sails.log.error(`SEND OTP ERROR: ${error}`);
Expand Down
4 changes: 2 additions & 2 deletions adapters/otp/default/defaultOTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export class DefaultOTP extends OneTimePasswordAdapter {
throw `otp generation error`
}

let mainLoginField = await Settings.get("LOGIN_FIELD") as string;
let mainLoginField = await Settings.get("LOGIN_FIELD") as string ?? 'phone';
if (NotificationManager.isChannelExist(mainLoginField === "phone" ? "sms" : mainLoginField)){
try {
await NotificationManager.sendMessageToUser("info", `Your code is ${otp.password}`,login);
await NotificationManager.sendMessageToUser("info", `Your code is ${otp.password}`,{ phone: { code: "", number:login}});
} catch (error) {
sails.log.error(`SEND OTP ERROR: ${error}`)
}
Expand Down
7 changes: 2 additions & 5 deletions libs/NotificationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NotificationManager {
await NotificationManager.send(badge, "manager", text, null);
}
catch (error) {
sails.log.info(`✉️ Notification manager > console: ${badge}, ${text}`);
sails.log.warn(`✉️ Notification manager > console: ${badge}, ${text}`);
}
}
/**
Expand Down Expand Up @@ -62,10 +62,7 @@ NotificationManager.send = async (badge, groupTo, message, user, type, subject,
}
sent = await channel.trySendMessage(badge, message, user, subject, data);
}
if (groupTo === "user" && user !== undefined && !sent) {
throw new Error(`Failed to send message to user ${user.login} with message: ${message}`);
}
else {
if (!sent) {
throw new Error(`Failed to send message to group ${groupTo}, ${type ? type : ""}, message: ${message}`);
}
};
Expand Down
8 changes: 2 additions & 6 deletions libs/NotificationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class NotificationManager {
try {
await NotificationManager.send(badge, "manager", text, null);
} catch (error) {
sails.log.info(`✉️ Notification manager > console: ${badge}, ${text}`)
sails.log.warn(`✉️ Notification manager > console: ${badge}, ${text}`)
}
}

Expand Down Expand Up @@ -111,14 +111,10 @@ export class NotificationManager {
continue;
}



sent = await channel.trySendMessage(badge, message, user, subject, data);
}

if (groupTo === "user" && user !== undefined && !sent) {
throw new Error(`Failed to send message to user ${user.login} with message: ${message}`);
} else {
if(!sent){
throw new Error(`Failed to send message to group ${groupTo}, ${type ? type: ""}, message: ${message}`);
}
};
Expand Down
9 changes: 7 additions & 2 deletions test/integration/notification_manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ class TestChannel extends NotificationManager_1.Channel {
}
const testChannel = new TestChannel;
describe("NotificationManager", function () {
it("NotificationManager add new channel", () => {
it("add new channel", () => {
NotificationManager_1.NotificationManager.registerChannel(testChannel);
if (NotificationManager_1.NotificationManager.channels.length !== 1)
throw `should 1`;
});
it("NotificationManager send message", () => {
it("send message", () => {
NotificationManager_1.NotificationManager.send("info", "user", "test123", null);
if (testChannel.lastMessage !== "test123")
throw `Problem in send Ntification`;
});
it("is exist", () => {
let result = NotificationManager_1.NotificationManager.isChannelExist('sms');
if (!result)
throw `Not exist??`;
});
});
9 changes: 7 additions & 2 deletions test/integration/notification_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ class TestChannel extends Channel {
const testChannel = new TestChannel;

describe("NotificationManager", function () {
it("NotificationManager add new channel", () => {
it("add new channel", () => {

NotificationManager.registerChannel(testChannel);
if (NotificationManager.channels.length !== 1) throw `should 1`
});


it("NotificationManager send message", () => {
it("send message", () => {
NotificationManager.send("info", "user", "test123", null);
if(testChannel.lastMessage !=="test123") throw `Problem in send Ntification`
});

it("is exist", () => {
let result = NotificationManager.isChannelExist('sms');
if(!result) throw `Not exist??`
});
});

0 comments on commit 354b03b

Please sign in to comment.