diff --git a/modules/worker/back/methods/worker/new.js b/modules/worker/back/methods/worker/new.js index bdab1335a..a57ca8c21 100644 --- a/modules/worker/back/methods/worker/new.js +++ b/modules/worker/back/methods/worker/new.js @@ -1,5 +1,4 @@ const md5 = require('md5'); -const {Email} = require('vn-print'); module.exports = Self => { Self.remoteMethodCtx('new', { @@ -10,113 +9,113 @@ module.exports = Self => { arg: 'fi', type: 'string', description: `The fi of worker`, - required: true + required: true, }, { arg: 'name', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'firstName', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'lastNames', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'email', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'roleFk', type: 'number', description: `REPLACE!`, - required: true + required: true, }, { arg: 'street', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'city', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'provinceFk', type: 'number', description: `REPLACE!`, - required: true + required: true, }, { arg: 'iban', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'bankEntityFk', type: 'number', description: `REPLACE!`, - required: true + required: true, }, { arg: 'companyFk', type: 'number', description: `REPLACE!`, - required: true + required: true, }, { arg: 'postcode', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'phone', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'code', type: 'string', description: `REPLACE!`, - required: true + required: true, }, { arg: 'bossFk', type: 'number', description: `REPLACE!`, - required: true + required: true, }, { arg: 'birth', type: 'date', description: `REPLACE!`, - required: true - } + required: true, + }, ], returns: { type: 'number', - root: true + root: true, }, http: { path: `/new`, - verb: 'POST' - } + verb: 'POST', + }, }); Self.new = async(ctx, options) => { @@ -126,8 +125,7 @@ module.exports = Self => { let tx; console.log(args); - if (typeof options == 'object') - Object.assign(myOptions, options); + if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); @@ -136,26 +134,38 @@ module.exports = Self => { let client; try { - client = await models.Client.findOne({ - where: {fi: args.fi}, - }, myOptions); + client = await models.Client.findOne( + { + where: {fi: args.fi}, + }, + myOptions + ); if (!client) { const nickname = args.firstName.concat(' ', args.lastNames); - const randomPassword = await models.Worker.rawSql('SELECT account.passwordGenerate();'); - const user = await models.Account.create({ - name: args.name, - nickname, - password: md5(randomPassword), - email: args.email, - role: args.role - }, myOptions); + const randomPassword = await models.Worker.rawSql( + 'SELECT account.passwordGenerate();' + ); + const user = await models.Account.create( + { + name: args.name, + nickname, + password: md5(randomPassword), + email: args.email, + role: args.role, + }, + myOptions + ); - await models.UserAccount.create({ - id: user.id - }, myOptions); + await models.UserAccount.create( + { + id: user.id, + }, + myOptions + ); - await models.Worker.rawSql('CALL vn.clientCreate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + await models.Worker.rawSql( + 'CALL vn.clientCreate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ args.firstName, args.lastNames, @@ -167,31 +177,38 @@ module.exports = Self => { args.companyFk, args.phone, args.email, - user.id - ] - , myOptions); + user.id, + ], + myOptions + ); - const address = await models.Address.create({ - clientFk: user.id, - street: args.street, - city: args.city, - provinceFk: args.provinceFk, - postalCode: args.postalCode, - mobile: args.phone, - nickname: 'TR ' + nickname, - isDefaultAddress: true - }, myOptions); + const address = await models.Address.create( + { + clientFk: user.id, + street: args.street, + city: args.city, + provinceFk: args.provinceFk, + postalCode: args.postalCode, + mobile: args.phone, + nickname: 'TR ' + nickname, + isDefaultAddress: true, + }, + myOptions + ); client = await models.Client.findById(user.id, null, myOptions); - console.log(address.id); - await client.updateAttributes({ - iban: args.iban, - bankEntityFk: args.bankEntityFk, - defaultAddressFk: address.id - }, myOptions); + await client.updateAttributes( + { + iban: args.iban, + bankEntityFk: args.bankEntityFk, + defaultAddressFk: address.id, + }, + myOptions + ); } - await models.Worker.rawSql('CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)', + await models.Worker.rawSql( + 'CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)', [ args.firstName, args.lastNames, @@ -199,9 +216,10 @@ module.exports = Self => { args.bossFk, client.id, args.fi, - args.birth - ] - , myOptions); + args.birth, + ], + myOptions + ); if (tx) await tx.commit(); } catch (e) { @@ -209,19 +227,7 @@ module.exports = Self => { throw e; } - // TODO: create this email, use client-welcome as template. And view CALL mail_insert in redmine for the body - // TODO: or use same funcionality back/methods/account/recover-password.js - - // TODO: call worerWelcomeEmail, and this is who create the url for change password - - const email = new Email('worker-welcome', { - recipient: args.email, - lang: ctx.req.getLocale() - }); - - await email.send(); - - // return id, and in front use for redirection + await models.Worker.workerWelcomeEmail(client.id); return client.id; }; }; diff --git a/modules/worker/back/methods/worker/workerWelcomeEmail.js b/modules/worker/back/methods/worker/workerWelcomeEmail.js new file mode 100644 index 000000000..7462df34d --- /dev/null +++ b/modules/worker/back/methods/worker/workerWelcomeEmail.js @@ -0,0 +1,78 @@ +const {Email} = require('vn-print'); + +module.exports = Self => { + Self.remoteMethodCtx('workerWelcomeEmail', { + description: + 'Sends the welcome email to the new worker', + accessType: 'WRITE', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The worker id', + http: {source: 'path'}, + } + ], + returns: { + type: ['object'], + root: true, + }, + http: { + path: '/:id/worker-welcome-email', + verb: 'POST', + }, + }); + + Self.workerWelcomeEmail = async ctx => { + const models = Self.app.models; + const userId = ctx.req.accessToken.userId; + const $t = ctx.req.__; // $translate + const origin = ctx.req.headers.origin; + + const args = Object.assign({}, ctx.args); + const params = { + recipient: args.recipient, + lang: ctx.req.getLocale(), + }; + + delete args.ctx; + for (const param in args) params[param] = args[param]; + + const claim = await models.Claim.findById(args.id, { + fields: ['id', 'clientFk'], + include: { + relation: 'client', + scope: { + fields: ['name', 'salesPersonFk'], + }, + }, + }); + + const message = $t('Claim pickup order sent', { + claimId: args.id, + clientName: claim.client().name, + claimUrl: `${origin}/#!/claim/${args.id}/summary`, + }); + + const salesPersonId = claim.client().salesPersonFk; + if (salesPersonId) + await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); + + await models.ClaimLog.create({ + originFk: args.id, + userFk: userId, + action: 'insert', + description: 'Claim-pickup-order sent', + changedModel: 'Mail', + }); + + const email = new Email('worker-welcome', { + recipient: args.email, + lang: ctx.req.getLocale(), + }); + + await email.send(); + // TODO: or use same funcionality back/methods/account/recover-password.js + }; +}; diff --git a/print/templates/email/worker-welcome/worker-welcome.html b/print/templates/email/worker-welcome/worker-welcome.html index 6aaee6120..f91c9941d 100644 --- a/print/templates/email/worker-welcome/worker-welcome.html +++ b/print/templates/email/worker-welcome/worker-welcome.html @@ -3,7 +3,6 @@
{{ $t('description.dearWorker') }},
-{{ $t('workerData', this.user.name, this.url) }}