264 lines
8.2 KiB
JavaScript
264 lines
8.2 KiB
JavaScript
/* eslint max-len: ["error", { "code": 150 }]*/
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('new', {
|
|
description: 'Creates a new worker and returns the id',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'fi',
|
|
type: 'string',
|
|
description: `The worker fi`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'name',
|
|
type: 'string',
|
|
description: `The user name`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'firstName',
|
|
type: 'string',
|
|
description: `The worker firstname`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'lastNames',
|
|
type: 'string',
|
|
description: `The worker lastnames`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'email',
|
|
type: 'string',
|
|
description: `The worker email`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'street',
|
|
type: 'string',
|
|
description: `The worker address`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'city',
|
|
type: 'string',
|
|
description: `The worker city`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'provinceFk',
|
|
type: 'number',
|
|
description: `The worker province`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'companyFk',
|
|
type: 'number',
|
|
description: `The worker company`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'postcode',
|
|
type: 'string',
|
|
description: `The worker postcode`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'phone',
|
|
type: 'string',
|
|
description: `The worker phone`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'code',
|
|
type: 'string',
|
|
description: `The worker code`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'bossFk',
|
|
type: 'number',
|
|
description: `The worker boss`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'birth',
|
|
type: 'date',
|
|
description: `The worker birth`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'payMethodFk',
|
|
type: 'number',
|
|
description: `The client payMethod`,
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'iban',
|
|
type: 'string',
|
|
description: `The client iban`,
|
|
},
|
|
{
|
|
arg: 'bankEntityFk',
|
|
type: 'number',
|
|
description: `The client bank entity`,
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'number',
|
|
root: true,
|
|
},
|
|
http: {
|
|
path: `/new`,
|
|
verb: 'POST',
|
|
},
|
|
});
|
|
|
|
Self.new = async(ctx, options) => {
|
|
const models = Self.app.models;
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
|
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: args.fi},
|
|
},
|
|
myOptions
|
|
);
|
|
|
|
if (!client) {
|
|
const nickname = args.firstName.concat(' ', args.lastNames);
|
|
const workerConfig = await models.WorkerConfig.findOne({fields: ['roleFk', 'businessTypeFk']});
|
|
const [randomPassword] = await models.Worker.rawSql(
|
|
'SELECT account.passwordGenerate() as password;'
|
|
);
|
|
|
|
const user = await models.VnUser.create(
|
|
{
|
|
name: args.name,
|
|
nickname,
|
|
password: randomPassword.password,
|
|
email: args.email,
|
|
roleFk: workerConfig.roleFk,
|
|
},
|
|
myOptions
|
|
);
|
|
|
|
await models.Account.create(
|
|
{
|
|
id: user.id,
|
|
},
|
|
myOptions
|
|
);
|
|
|
|
const payMethod = await models.PayMethod.findById(args.payMethodFk, {fields: ['isIbanRequiredForClients']});
|
|
if (payMethod.isIbanRequiredForClients && !args.iban)
|
|
throw new UserError(`That payment method requires an IBAN`);
|
|
|
|
await models.Worker.rawSql(
|
|
'CALL vn.client_create(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
[
|
|
args.firstName,
|
|
args.lastNames,
|
|
args.fi,
|
|
args.street,
|
|
args.postcode,
|
|
args.city,
|
|
args.provinceFk,
|
|
args.companyFk,
|
|
args.phone,
|
|
args.email,
|
|
user.id,
|
|
],
|
|
myOptions
|
|
);
|
|
|
|
const address = await models.Address.create(
|
|
{
|
|
clientFk: user.id,
|
|
street: args.street,
|
|
city: args.city,
|
|
provinceFk: args.provinceFk,
|
|
postalCode: args.postcode,
|
|
mobile: args.phone,
|
|
nickname: nickname,
|
|
isDefaultAddress: true,
|
|
},
|
|
myOptions
|
|
);
|
|
|
|
client = await models.Client.findById(
|
|
user.id,
|
|
{fields: ['id', 'name', 'socialName', 'street', 'city', 'iban', 'bankEntityFk', 'defaultAddressFk', 'businessTypeFk', 'fi']},
|
|
myOptions
|
|
);
|
|
|
|
await client.updateAttributes(
|
|
{
|
|
payMethod: args.payMethod,
|
|
iban: args.iban,
|
|
bankEntityFk: args.bankEntityFk,
|
|
defaultAddressFk: address.id,
|
|
businessTypeFk: workerConfig.businessTypeFk,
|
|
},
|
|
myOptions
|
|
);
|
|
}
|
|
|
|
const user = await models.VnUser.findById(client.id, null, myOptions);
|
|
await user.updateAttribute('email', args.email, myOptions);
|
|
|
|
await models.Worker.create({
|
|
id: client.id,
|
|
code: args.code,
|
|
firstName: args.firstName,
|
|
lastName: args.lastNames,
|
|
bossFk: args.bossFk,
|
|
fi: args.fi,
|
|
birth: args.birth,
|
|
|
|
}, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
} catch (error) {
|
|
if (tx) await tx.rollback();
|
|
const code = error.code;
|
|
const message = error.sqlMessage;
|
|
|
|
if (error.message && error.message.includes(`Email already exists`))
|
|
throw new UserError(`This personal mail already exists`);
|
|
|
|
if (code === 'ER_DUP_ENTRY' && message.includes(`CodigoTrabajador_UNIQUE`))
|
|
throw new UserError(`This worker code already exists`);
|
|
|
|
if (code === 'ER_DUP_ENTRY' && message.includes(`PRIMARY`))
|
|
throw new UserError(`This worker already exists`);
|
|
|
|
throw error;
|
|
}
|
|
|
|
await models.VnUser.resetPassword({
|
|
email: args.email,
|
|
emailTemplate: 'worker-welcome',
|
|
id: client.id
|
|
});
|
|
|
|
return {id: client.id};
|
|
};
|
|
};
|