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 {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: {
alexm marked this conversation as resolved Outdated
Outdated
Review

El campo se rellena en Access pero en el codigo no se utiliza para nada, seguir usandolo en Salix o quitarlo?

El campo se rellena en Access pero en el codigo no se utiliza para nada, seguir usandolo en Salix o quitarlo?

Eliminar campo

Eliminar campo
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,
},
alexm marked this conversation as resolved Outdated

preguntar a laboral si lo quitamos de la creacion.
Preguntar si al crearlo alguna vez ponen algo distinto a employee

preguntar a laboral si lo quitamos de la creacion. Preguntar si al crearlo alguna vez ponen algo distinto a employee
Outdated
Review

Quitar

Quitar
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(
alexm marked this conversation as resolved Outdated

Preguntar a Alicia si esto aun es útil

Preguntar a Alicia si esto aun es útil
{
iban: args.iban,
bankEntityFk: args.bankEntityFk,
defaultAddressFk: address.id,
},
myOptions
alexm marked this conversation as resolved Outdated

preguntar si en estos casos sí es posible el null

preguntar si en estos casos sí es posible el null
);
}
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;
};
};

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">
<h1>{{ $t('title', [id]) }}</h1>
<p>{{ $t('description.dearWorker') }},</p>
<p v-html="instructions"></p>
<p>{{ $t('workerData', this.user.name, this.url) }}</p>
</div>
</div>