4658-createWorker #1236

Merged
alexm merged 35 commits from 4658-createWorker into dev 2023-01-23 07:30:52 +00:00
3 changed files with 159 additions and 76 deletions
Showing only changes of commit 7a7da9508f - Show all commits

View File

@ -1,5 +1,4 @@
const md5 = require('md5'); const md5 = require('md5');
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('new', { Self.remoteMethodCtx('new', {
@ -10,113 +9,113 @@ module.exports = Self => {
arg: 'fi', arg: 'fi',
type: 'string', type: 'string',
description: `The fi of worker`, description: `The fi of worker`,
required: true required: true,
}, },
{ {
arg: 'name', arg: 'name',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'firstName', arg: 'firstName',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'lastNames', arg: 'lastNames',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'email', arg: 'email',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'roleFk', arg: 'roleFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'street', arg: 'street',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'city', arg: 'city',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'provinceFk', arg: 'provinceFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'iban', arg: 'iban',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'bankEntityFk', arg: 'bankEntityFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'companyFk', arg: 'companyFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'postcode', arg: 'postcode',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'phone', arg: 'phone',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'code', arg: 'code',
type: 'string', type: 'string',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'bossFk', arg: 'bossFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
}, },
{ {
arg: 'birth', arg: 'birth',
type: 'date', type: 'date',
description: `REPLACE!`, description: `REPLACE!`,
required: true required: true,
} },
], ],
returns: { returns: {
type: 'number', type: 'number',
root: true root: true,
}, },
http: { http: {
path: `/new`, path: `/new`,
verb: 'POST' verb: 'POST',
} },
}); });
Self.new = async(ctx, options) => { Self.new = async(ctx, options) => {
@ -126,8 +125,7 @@ module.exports = Self => {
let tx; let tx;
console.log(args); console.log(args);
if (typeof options == 'object') if (typeof options == 'object') Object.assign(myOptions, options);
Object.assign(myOptions, options);
if (!myOptions.transaction) { if (!myOptions.transaction) {
tx = await Self.beginTransaction({}); tx = await Self.beginTransaction({});
@ -136,26 +134,38 @@ module.exports = Self => {
let client; let client;
try { try {
client = await models.Client.findOne({ client = await models.Client.findOne(
{
where: {fi: args.fi}, where: {fi: args.fi},
}, myOptions); },
myOptions
);
if (!client) { if (!client) {
const nickname = args.firstName.concat(' ', args.lastNames); const nickname = args.firstName.concat(' ', args.lastNames);
const randomPassword = await models.Worker.rawSql('SELECT account.passwordGenerate();'); const randomPassword = await models.Worker.rawSql(
const user = await models.Account.create({ 'SELECT account.passwordGenerate();'
);
const user = await models.Account.create(
{
name: args.name, name: args.name,
nickname, nickname,
password: md5(randomPassword), password: md5(randomPassword),
email: args.email, email: args.email,
role: args.role role: args.role,
}, myOptions); },
myOptions
);
await models.UserAccount.create({ await models.UserAccount.create(
id: user.id {
}, myOptions); id: user.id,
},
myOptions
);
await models.Worker.rawSql('CALL vn.clientCreate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', await models.Worker.rawSql(
'CALL vn.clientCreate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[ [
args.firstName, args.firstName,
args.lastNames, args.lastNames,
@ -167,11 +177,13 @@ module.exports = Self => {
args.companyFk, args.companyFk,
args.phone, args.phone,
args.email, args.email,
user.id user.id,
] ],
, myOptions); myOptions
);
const address = await models.Address.create({ const address = await models.Address.create(
{
clientFk: user.id, clientFk: user.id,
street: args.street, street: args.street,
city: args.city, city: args.city,
@ -179,19 +191,24 @@ module.exports = Self => {
postalCode: args.postalCode, postalCode: args.postalCode,
mobile: args.phone, mobile: args.phone,
nickname: 'TR ' + nickname, nickname: 'TR ' + nickname,
isDefaultAddress: true isDefaultAddress: true,
}, myOptions); },
myOptions
);
client = await models.Client.findById(user.id, null, myOptions); client = await models.Client.findById(user.id, null, myOptions);
console.log(address.id); await client.updateAttributes(
await client.updateAttributes({ {
iban: args.iban, iban: args.iban,
bankEntityFk: args.bankEntityFk, bankEntityFk: args.bankEntityFk,
defaultAddressFk: address.id defaultAddressFk: address.id,
}, myOptions); },
myOptions
);
} }
await models.Worker.rawSql('CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)', await models.Worker.rawSql(
'CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)',
[ [
args.firstName, args.firstName,
args.lastNames, args.lastNames,
@ -199,9 +216,10 @@ module.exports = Self => {
args.bossFk, args.bossFk,
client.id, client.id,
args.fi, args.fi,
args.birth args.birth,
] ],
, myOptions); myOptions
);
if (tx) await tx.commit(); if (tx) await tx.commit();
} catch (e) { } catch (e) {
@ -209,19 +227,7 @@ module.exports = Self => {
throw e; throw e;
} }
// TODO: create this email, use client-welcome as template. And view CALL mail_insert in redmine for the body await models.Worker.workerWelcomeEmail(client.id);
// 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
return client.id; return client.id;
}; };
}; };

View File

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

View File

@ -3,7 +3,6 @@
<div class="grid-block vn-pa-ml"> <div class="grid-block vn-pa-ml">
<h1>{{ $t('title', [id]) }}</h1> <h1>{{ $t('title', [id]) }}</h1>
<p>{{ $t('description.dearWorker') }},</p> <p>{{ $t('description.dearWorker') }},</p>
<p v-html="instructions"></p>
<p>{{ $t('workerData', this.user.name, this.url) }}</p> <p>{{ $t('workerData', this.user.name, this.url) }}</p>
</div> </div>
</div> </div>