salix/modules/worker/back/methods/worker/specs/new.spec.js

143 lines
4.5 KiB
JavaScript

const {models} = require('vn-loopback/server/server');
describe('Worker new', () => {
const developerId = 9;
const employeeId = 1;
const bruceWayneId = 1101;
const accessToken = {accessToken: {userId: developerId}};
const ctx = {req: accessToken};
let tx;
let opts;
beforeAll.mockLoopBackContext();
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']});
try {
await createWorker(ctx, opts, {email: user.email});
} catch (e) {
expect(e.message).toEqual('This personal mail already exists');
}
});
it('if worker code already exists', async() => {
const worker = await models.Worker.findById(employeeId, {fields: ['code']});
try {
await createWorker(ctx, opts, {code: worker.code});
} catch (e) {
expect(e.message).toEqual('This worker code already exists');
}
});
it('if worker already exists', async() => {
const worker = await models.Client.findById(employeeId, {fields: ['fi']});
try {
await createWorker(ctx, opts, {fi: worker.fi});
} catch (e) {
expect(e.message).toEqual('This worker already exists');
}
});
it('if payMethod require iban', async() => {
const payMethodIbanRequired = await models.PayMethod.findOne({
fields: ['id'], where: {isIbanRequiredForClients: true}
});
try {
await createWorker(ctx, opts, {payMethodFk: payMethodIbanRequired.id});
} catch (e) {
expect(e.message).toEqual('That payment method requires an IBAN');
}
});
});
it('should create a new worker', async() => {
let newWorker;
try {
newWorker = await createWorker(ctx);
expect(newWorker.id).toBeDefined();
} finally {
await removeWorker(newWorker.id);
}
});
it('should create a new client', async() => {
let newWorker;
try {
newWorker = await createWorker(ctx);
let client = await models.Client.findById(newWorker.id);
expect(client).toBeDefined();
} finally {
await removeWorker(newWorker.id);
}
});
it('should create a new worker in client', async() => {
const client = await models.Client.findById(bruceWayneId, {fields: ['fi', 'email']});
let newWorker;
try {
newWorker = await createWorker(ctx, undefined, {fi: client.fi, email: client.email});
expect(newWorker.id).toEqual(bruceWayneId);
} finally {
await models.Worker.destroyById(newWorker.id);
}
});
it('should create a new external worker', async() => {
let newWorker;
try {
newWorker = await createWorker(ctx, undefined, {isFreelance: true});
const client = await models.Client.findById(newWorker.id);
expect(newWorker.id).toBeDefined();
expect(client).toBeNull();
} finally {
await removeWorker(newWorker.id);
}
});
});
async function removeWorker(id) {
await models.Worker.destroyById(id);
await models.Address.destroyAll({clientFk: id});
await models.Mandate.destroyAll({clientFk: 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
);
}