7631_testToMaster_2426 #2634

Merged
alexm merged 147 commits from 7631_testToMaster_2426 into master 2024-06-25 06:36:59 +00:00
3 changed files with 268 additions and 368 deletions
Showing only changes of commit a79285cd3d - Show all commits

View File

@ -5,108 +5,80 @@ module.exports = Self => {
Self.remoteMethodCtx('new', {
description: 'Creates a new worker and returns the id',
accessType: 'WRITE',
accepts: [
{
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`,
}
],
}, {
arg: 'isFreelance',
type: 'boolean',
}],
returns: {
type: 'number',
root: true,
@ -117,11 +89,30 @@ module.exports = Self => {
},
});
Self.new = async(ctx, options) => {
Self.new = async(
ctx,
fi,
name,
firstName,
lastNames,
email,
street,
city,
provinceFk,
companyFk,
postcode,
phone,
code,
bossFk,
birth,
payMethodFk,
iban,
bankEntityFk,
isFreelance,
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);
@ -132,131 +123,103 @@ module.exports = Self => {
}
let client;
let user;
try {
client = await models.Client.findOne(
{
where: {fi: args.fi},
},
myOptions
);
client = await models.Client.findOne({where: {fi}}, myOptions);
const nickname = firstName.concat(' ', lastNames);
const {roleFk, businessTypeFk} = await models.WorkerConfig.findOne({fields: ['roleFk', 'businessTypeFk']});
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;'
);
if (!isFreelance)
if (!payMethodFk) throw new UserError('Payment method is required');
const user = await models.VnUser.create(
{
name: args.name,
if (isFreelance || !client) {
const [{password}] = await models.Worker.rawSql('SELECT account.passwordGenerate() as password;');
user = await models.VnUser.create({
name,
nickname,
password: randomPassword.password,
email: args.email,
roleFk: workerConfig.roleFk,
},
myOptions
);
password,
email,
roleFk,
}, myOptions);
await models.Account.create(
{
id: user.id,
},
myOptions
);
await models.Account.create({
id: user.id
}, myOptions);
} else if (client) user = await models.VnUser.findById(client.id, null, 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`);
if (!client && !isFreelance) {
const payMethod = await models.PayMethod.findById(payMethodFk, {fields: ['isIbanRequiredForClients']});
if (payMethod.isIbanRequiredForClients && !iban) throw new UserError('That payment method requires an IBAN');
await models.Worker.rawSql(
'CALL vn.client_create(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
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,
firstName,
lastNames,
fi,
street,
postcode,
city,
provinceFk,
companyFk,
phone,
email,
user.id,
],
myOptions
);
myOptions);
const address = await models.Address.create(
{
const address = await models.Address.create({
clientFk: user.id,
street: args.street,
city: args.city,
provinceFk: args.provinceFk,
postalCode: args.postcode,
mobile: args.phone,
street: street,
city: city,
provinceFk: provinceFk,
postalCode: postcode,
mobile: phone,
nickname: nickname,
isDefaultAddress: true,
},
myOptions
);
}, myOptions);
client = await models.Client.findById(
user.id,
{fields: ['id', 'name', 'socialName', 'street', 'city', 'iban', 'bankEntityFk', 'defaultAddressFk', 'businessTypeFk', 'fi']},
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,
await client.updateAttributes({
payMethod: payMethodFk,
iban,
bankEntityFk,
defaultAddressFk: address.id,
businessTypeFk: workerConfig.businessTypeFk,
},
myOptions
);
businessTypeFk,
}, myOptions);
}
const user = await models.VnUser.findById(client.id, null, myOptions);
await user.updateAttribute('email', args.email, myOptions);
await user.updateAttribute('email', 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,
id: isFreelance ? user.id : client.id,
firstName,
lastName: lastNames,
code,
bossFk,
fi,
birth,
}, myOptions);
if (tx) await tx.commit();
} catch (error) {
} catch (e) {
if (tx) await tx.rollback();
const code = error.code;
const message = error.sqlMessage;
const code = e.code;
const message = e.sqlMessage;
if (error.message && error.message.includes(`Email already exists`))
throw new UserError(`This personal mail already exists`);
if (e.message && e.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(`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`);
if (code === 'ER_DUP_ENTRY' && message.includes(`PRIMARY`)) throw new UserError(`This worker already exists`);
throw error;
throw e;
}
await models.VnUser.resetPassword({
email: args.email,
emailTemplate: 'worker-welcome',
id: client.id
});
await models.VnUser.resetPassword({email, emailTemplate: 'worker-welcome', id: client.id});
return {id: client.id};
};

View File

@ -1,189 +1,101 @@
const models = require('vn-loopback/server/server').models;
const {models} = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('Worker new', () => {
beforeAll(async() => {
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
const developerId = 9;
const employeeId = 1;
const defaultWorker = {
fi: '78457139E',
name: 'defaulterworker',
firstName: 'DEFAULT',
lastNames: 'WORKER',
email: 'defaultWorker@mydomain.com',
street: 'S/ DEFAULTWORKERSTREET',
city: 'defaultWorkerCity',
provinceFk: 1,
companyFk: 442,
postcode: '46680',
phone: '123456789',
code: 'DWW',
bossFk: 9,
birth: '2022-12-11T23:00:00.000Z',
payMethodFk: 1,
roleFk: 1
};
const bruceWayneId = 1101;
const accessToken = {accessToken: {userId: developerId}};
const ctx = {req: accessToken};
let tx;
let opts;
const req = {accessToken: {userId: 9}};
beforeAll(async() => {
const activeCtx = {accessToken, http: {req: {headers: {origin: 'http://localhost'}}}};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({active: activeCtx});
});
it('should return error if personal mail already exists', async() => {
describe('should return error', () => {
beforeEach(async() => {
tx = await models.Worker.beginTransaction({});
opts = {transaction: tx};
});
afterEach(async() => await tx.rollback());
it('if personal mail already exists', async() => {
const user = await models.VnUser.findById(employeeId, {fields: ['email']});
const tx = await models.Worker.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ctx = {
args: Object.assign({}, defaultWorker, {email: user.email}),
req
};
await models.Worker.new(ctx, options);
await tx.rollback();
await createWorker(ctx, opts, {email: user.email});
} catch (e) {
error = e;
await tx.rollback();
expect(e.message).toEqual('This personal mail already exists');
}
expect(error.message).toEqual('This personal mail already exists');
});
it('should return error if worker code already exists', async() => {
it('if worker code already exists', async() => {
const worker = await models.Worker.findById(employeeId, {fields: ['code']});
const tx = await models.Worker.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ctx = {
args: Object.assign({}, defaultWorker, {code: worker.code}),
req
};
await models.Worker.new(ctx, options);
await tx.rollback();
await createWorker(ctx, opts, {code: worker.code});
} catch (e) {
error = e;
await tx.rollback();
expect(e.message).toEqual('This worker code already exists');
}
expect(error.message).toEqual('This worker code already exists');
});
it('should return error if worker already exists', async() => {
it('if worker already exists', async() => {
const worker = await models.Client.findById(employeeId, {fields: ['fi']});
const tx = await models.Worker.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ctx = {
args: Object.assign({}, defaultWorker, {fi: worker.fi}),
req
};
await models.Worker.new(ctx, options);
await tx.rollback();
await createWorker(ctx, opts, {fi: worker.fi});
} catch (e) {
error = e;
await tx.rollback();
expect(e.message).toEqual('This worker already exists');
}
expect(error.message).toEqual('This worker already exists');
});
it('should return error if payMethod require iban', async() => {
it('if payMethod require iban', async() => {
const payMethodIbanRequired = await models.PayMethod.findOne({
where: {
isIbanRequiredForClients: true
},
fields: ['id']
fields: ['id'], where: {isIbanRequiredForClients: true}
});
const tx = await models.Worker.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ctx = {
args: Object.assign({}, defaultWorker, {payMethodFk: payMethodIbanRequired.id}),
req
};
await models.Worker.new(ctx, options);
await tx.rollback();
await createWorker(ctx, opts, {payMethodFk: payMethodIbanRequired.id});
} catch (e) {
error = e;
await tx.rollback();
expect(e.message).toEqual('That payment method requires an IBAN');
}
expect(error.message).toEqual('That payment method requires an IBAN');
});
});
it('should create a new worker', async() => {
let newWorker;
try {
newWorker = await models.Worker.new({args: defaultWorker, req});
newWorker = await createWorker(ctx);
expect(newWorker.id).toBeDefined();
} finally {
await removeWorker(newWorker.id);
}
expect(newWorker.id).toBeDefined();
});
it('should create a new client', async() => {
let newWorker;
let client;
try {
newWorker = await models.Worker.new({args: defaultWorker, req});
client = await models.Client.findById(newWorker.id);
newWorker = await createWorker(ctx);
let client = await models.Client.findById(newWorker.id);
expect(client).toBeDefined();
} finally {
await removeWorker(newWorker.id);
}
expect(client).toBeDefined();
});
it('should create a new worker in client', async() => {
const bruceWayneId = 1101;
const client = await models.Client.findById(bruceWayneId, {fields: ['fi', 'email']});
const newWorkerData = {
args: Object.assign(
{},
defaultWorker,
{
fi: client.fi,
email: client.email
}),
req
};
let newWorker;
try {
newWorker = await models.Worker.new(newWorkerData);
newWorker = await createWorker(ctx, undefined, {fi: client.fi, email: client.email});
expect(newWorker.id).toEqual(bruceWayneId);
} finally {
await models.Worker.destroyById(newWorker.id);
}
expect(newWorker.id).toEqual(bruceWayneId);
});
});
@ -194,3 +106,28 @@ async function removeWorker(id) {
await models.Client.destroyById(id);
await models.VnUser.destroyById(id);
}
async function createWorker(ctx, opts = undefined, params = {}) {
return models.Worker.new(
ctx,
params.fi ?? '78457139E',
params.name ?? 'defaulterworker',
params.firstName ?? 'DEFAULT',
params.lastNames ?? 'WORKER',
params.email ?? 'defaultWorker@mydomain.com',
params.street ?? 'S/ DEFAULTWORKERSTREET',
params.city ?? 'defaultWorkerCity',
params.provinceFk ?? 1,
params.companyFk ?? 442,
params.postcode ?? '46680',
params.phone ?? '123456789',
params.code ?? 'DWW',
params.bossFk ?? 9,
params.birth ?? '2022-12-11T23:00:00.000Z',
params.payMethodFk ?? 1,
undefined,
undefined,
params.isFreelance ?? false,
opts
);
}

View File

@ -46,7 +46,8 @@
"type": "number"
},
"code": {
"type" : "string"
"type": "string",
"required": true
},
"fi": {
"type": "string"
@ -78,7 +79,6 @@
"isSsDiscounted": {
"type": "boolean"
}
},
"relations": {
"user": {