feat(send): use vn-print libary and adapt test
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
de44ce6bd4
commit
c637f2c879
|
@ -1,4 +1,5 @@
|
|||
const axios = require('axios');
|
||||
const {Email} = require('vn-print');
|
||||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethod('send', {
|
||||
|
@ -15,27 +16,18 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.send = async options => {
|
||||
// const headers = ctx.req.headers;
|
||||
// const origin = headers.origin || 'http://' + headers.host;
|
||||
// const auth = ctx.req.accessToken;
|
||||
// console.log(origin);
|
||||
if (process.env.NODE_ENV == 'test')
|
||||
throw new UserError(`Action not allowed on the test environment`);
|
||||
|
||||
const models = Self.app.models;
|
||||
const status = 'pending';
|
||||
const findStatus = 'pending';
|
||||
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const notificationQueue = await models.NotificationQueue.find({
|
||||
where: {status: status},
|
||||
where: {status: findStatus},
|
||||
include: [
|
||||
{
|
||||
relation: 'notification',
|
||||
|
@ -46,7 +38,7 @@ module.exports = Self => {
|
|||
include: {
|
||||
relation: 'user',
|
||||
scope: {
|
||||
fields: ['name']
|
||||
fields: ['name', 'email', 'lang']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,24 +51,33 @@ module.exports = Self => {
|
|||
|
||||
const statusSent = 'sent';
|
||||
const statusError = 'error';
|
||||
|
||||
for (const queue of notificationQueue) {
|
||||
// console.log(queue);
|
||||
// console.log(origin);
|
||||
// console.log(auth);
|
||||
// console.log(queue.notification().name);
|
||||
// console.log(queue.params);
|
||||
const queueName = queue.notification().name;
|
||||
const queueParams = JSON.parse(queue.params);
|
||||
|
||||
// const queueParams = Object.assign({}, JSON.parse(queue.params));
|
||||
// queueParams.authorization = auth.id;
|
||||
for (const notificationUser of queue.notification().subscription()) {
|
||||
try {
|
||||
const sendParams = {
|
||||
recipient: notificationUser.user().email,
|
||||
lang: notificationUser.user().lang
|
||||
};
|
||||
|
||||
try {
|
||||
// await print axios.get(`print`, queueParams)
|
||||
await queue.updateAttribute('status', statusSent);
|
||||
} catch (error) {
|
||||
await queue.updateAttribute('status', statusError);
|
||||
if (notificationUser.userFk == queue.authorFk) {
|
||||
await queue.updateAttribute('status', statusSent);
|
||||
continue;
|
||||
}
|
||||
|
||||
const newParams = Object.assign({}, queueParams, sendParams);
|
||||
const email = new Email(queueName, newParams);
|
||||
|
||||
await email.send();
|
||||
|
||||
await queue.updateAttribute('status', statusSent);
|
||||
} catch (error) {
|
||||
await queue.updateAttribute('status', statusError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return notificationQueue;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -27,7 +27,7 @@ describe('Notification Send()', () => {
|
|||
throw e;
|
||||
}
|
||||
|
||||
expect(before.length).toEqual(1);
|
||||
expect(before.length).toEqual(3);
|
||||
expect(after.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,8 +7,8 @@ CREATE TABLE notification(
|
|||
);
|
||||
|
||||
CREATE TABLE notificationAcl(
|
||||
notificationFk INT, -- FK notification.id
|
||||
roleFk INT(10) unsigned, -- FK account.role.id
|
||||
notificationFk INT,
|
||||
roleFk INT(10) unsigned,
|
||||
PRIMARY KEY(notificationFk, roleFk)
|
||||
);
|
||||
|
||||
|
@ -21,8 +21,8 @@ ALTER TABLE `util`.`notificationAcl` ADD CONSTRAINT `notificationAcl_ibfk_2` FOR
|
|||
ON UPDATE CASCADE;
|
||||
|
||||
CREATE TABLE notificationSubscription(
|
||||
notificationFk INT, -- FK notification.id
|
||||
userFk INT(10) unsigned, -- FK account.user.id
|
||||
notificationFk INT,
|
||||
userFk INT(10) unsigned,
|
||||
PRIMARY KEY(notificationFk, userFk)
|
||||
);
|
||||
|
||||
|
@ -36,9 +36,9 @@ ALTER TABLE `util`.`notificationSubscription` ADD CONSTRAINT `notificationSubscr
|
|||
|
||||
CREATE TABLE notificationQueue(
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
notificationFk VARCHAR(255), -- FK notification.name
|
||||
params TEXT, -- JSON
|
||||
authorFk INT(10) unsigned NULL, -- FK account.user.id
|
||||
notificationFk VARCHAR(255),
|
||||
params JSON,
|
||||
authorFk INT(10) unsigned NULL,
|
||||
`status` ENUM('pending', 'sent', 'error') NOT NULL DEFAULT 'pending',
|
||||
created DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
INDEX(notificationFk),
|
||||
|
|
|
@ -2653,7 +2653,7 @@ INSERT INTO `util`.`notificationConfig`
|
|||
|
||||
INSERT INTO `util`.`notification` (`id`, `name`, `description`)
|
||||
VALUES
|
||||
(1, 'invoice', 'notification fixture one');
|
||||
(1, 'print-email', 'notification fixture one');
|
||||
|
||||
INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`)
|
||||
VALUES
|
||||
|
@ -2661,11 +2661,15 @@ INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`)
|
|||
|
||||
INSERT INTO `util`.`notificationQueue` (`id`, `notificationFk`, `params`, `authorFk`, `status`, `created`)
|
||||
VALUES
|
||||
(1, 'invoice', '{"invoiceId": 1}', 9, 'pending', util.VN_CURDATE());
|
||||
(1, 'print-email', '{"id": "1"}', 9, 'pending', util.VN_CURDATE()),
|
||||
(2, 'print-email', '{"id": "2"}', null, 'pending', util.VN_CURDATE()),
|
||||
(3, 'print-email', null, null, 'pending', util.VN_CURDATE());
|
||||
|
||||
INSERT INTO `util`.`notificationSubscription` (`notificationFk`, `userFk`)
|
||||
VALUES
|
||||
(1, 1109),
|
||||
(1, 1110);
|
||||
|
||||
INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`)
|
||||
VALUES
|
||||
(1, 9);
|
||||
|
|
Loading…
Reference in New Issue