diff --git a/modules/worker/back/methods/worker/new.js b/modules/worker/back/methods/worker/new.js new file mode 100644 index 000000000..481011563 --- /dev/null +++ b/modules/worker/back/methods/worker/new.js @@ -0,0 +1,197 @@ +const md5 = require('md5'); +const {Email} = require('vn-print'); + +module.exports = Self => { + Self.remoteMethodCtx('new', { + description: 'Creates a new ticket and returns the id', + accessType: 'WRITE', + accepts: [ + { + arg: 'fi', + type: 'number', + description: `The fi of worker`, + required: true + }, + { + arg: 'name', + type: 'string', + description: `REPLACE!`, + required: true + }, + { + arg: 'firstName', + type: 'string', + description: `REPLACE!`, + required: true + }, + { + arg: 'lastNames', + type: 'string', + description: `REPLACE!`, + required: true + }, + { + arg: 'password', + type: 'string', + description: `REPLACE!`, + required: true + }, + { + arg: 'email', + type: 'string', + description: `REPLACE!`, + required: true + }, + { + arg: 'role', + type: 'number', + description: `REPLACE!` + }, + { + arg: 'street', + type: 'string', + description: `REPLACE!` + }, + { + arg: 'string', + type: 'number', + description: `REPLACE!` + }, + { + arg: 'provinceFk', + type: 'number', + description: `REPLACE!` + }, + { + arg: 'postalCode', + type: 'number', + description: `REPLACE!` + }, + { + arg: 'phone', + type: 'number', + description: `REPLACE!` + }, + { + arg: 'workerCode', + type: 'string', + description: `REPLACE!` + }, + { + arg: 'bossFk', + type: 'number', + description: `REPLACE!` + }, + { + arg: 'birth', + type: 'date', + description: `REPLACE!` + } + ], + returns: { + type: 'number', + root: true + }, + http: { + path: `/new`, + verb: 'POST' + } + }); + + Self.new = async(ctx, options) => { + const models = Self.app.models; + const myOptions = {}; + const args = ctx.args; + + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + let client; + try { + client = await models.Client.findOne({ + where: {fi: fi}, + }, myOptions); + + if (!client) { + const nickname = args.firstName.concat(' ', args.lastNames); + const user = await models.Account.create({ + name: args.name, + nickname, + password: md5(args.password), + email: args.email, + role: args.role + }, myOptions); + + await models.UserAccount.create({ + id: user.id + }, myOptions); + + await models.Worker.rawSql('CALL vn.clientCreate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + [ + args.firstName, + args.lastNames, + args.fi, + args.address, + args.postalCode, + args.town, + args.province, + args.company, + args.phone, + args.email, + user.id + ] + , myOptions); + + const address = await models.Address.create({ + clientFk: user.id, + address: args.street, + town: args.town, + provinceFk: args.provinceFk, + postalCode: args.postalCode, + mobile: args.phone, + nickname: 'TR ' + nickname, + isDefaultAddress: true + }, myOptions); + + client = await models.Sale.findById(user.id, null, myOptions); + await client.updateAttribute('defaultAddressFk ', address.id); + } + + await models.Worker.rawSql('CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)', + [ + args.firstName, + args.lastNames, + args.workerCode, + args.bossFk, + user.id, + args.fi, + args.birth + ] + , myOptions); + + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + 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 + const email = new Email('worker-welcome', { + recipient: args.email, + lang: ctx.req.getLocale() + }); + + await email.send(); + + // return id, and in front use for redirection + return client.id; + }; +}; diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js index ec6c4af28..e66259cd0 100644 --- a/modules/worker/back/models/worker.js +++ b/modules/worker/back/models/worker.js @@ -13,4 +13,5 @@ module.exports = Self => { require('../methods/worker/contracts')(Self); require('../methods/worker/holidays')(Self); require('../methods/worker/activeContract')(Self); + require('../methods/worker/new')(Self); };