4658-createWorker #1236

Merged
alexm merged 35 commits from 4658-createWorker into dev 2023-01-23 07:30:52 +00:00
2 changed files with 198 additions and 0 deletions
Showing only changes of commit 84a5f3986b - Show all commits

View File

@ -0,0 +1,197 @@
const md5 = require('md5');
const {Email} = require('vn-print');
module.exports = Self => {
Self.remoteMethodCtx('new', {
alexm marked this conversation as resolved
Review

canvia la descripcio

canvia la descripcio
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({
alexm marked this conversation as resolved
Review

const

const
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;
};
};

View File

@ -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);
};