const app = require(`../../../ticket/server/server`); const server = require(`../../../loopback/server/server`); const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL; describe('ticket ticketCreateWithUser()', () => { const today = new Date(); it('should confirm the procedure creates the expected ticket', async () => { let stmts = []; let stmt; stmts.push('START TRANSACTION'); let params = { clientFk: 101, shipped: today, warehouseFk: 1, companyFk: 442, addressFk: 1, agencyModeFk: 2, routeFk: null, landed: today, userId: 18 }; stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, params.companyFk, params.addressFk, params.agencyModeFk, params.routeFk, params.landed, params.userId ]); stmts.push(stmt); let ticketResultIndex = stmts.push(`SELECT * FROM vn.ticket WHERE id = @newTicketId`) - 1; stmts.push('ROLLBACK'); let sql = ParameterizedSQL.join(stmts, ';'); let result = await app.models.Ticket.rawStmt(sql); let ticketResult = result[ticketResultIndex][0]; expect(ticketResult.id).toBeGreaterThan(21); expect(ticketResult.clientFk).toEqual(params.clientFk); expect(ticketResult.warehouseFk).toEqual(params.warehouseFk); expect(ticketResult.companyFk).toEqual(params.companyFk); expect(ticketResult.addressFk).toEqual(params.addressFk); expect(ticketResult.agencyModeFk).toEqual(params.agencyModeFk); expect(ticketResult.routeFk).toEqual(params.routeFk); }); it('should confirm the procedure creates the expected observations in the ticket', async () => { let stmts = []; let stmt; stmts.push('START TRANSACTION'); let params = { clientFk: 101, shipped: today, warehouseFk: 1, companyFk: 442, addressFk: 1, agencyModeFk: 2, routeFk: null, landed: today, userId: 18 }; stmt = new ParameterizedSQL('CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)', [ params.clientFk, params.shipped, params.warehouseFk, params.companyFk, params.addressFk, params.agencyModeFk, params.routeFk, params.landed, params.userId ]); stmts.push(stmt); let ticketObsevationsIndex = stmts.push(`SELECT * FROM vn.ticketObservation WHERE ticketFk = @newTicketId`) - 1; stmts.push('ROLLBACK'); let sql = ParameterizedSQL.join(stmts, ';'); let result = await app.models.Ticket.rawStmt(sql); let firstTicketObservation = result[ticketObsevationsIndex][0]; let secondTicketObservation = result[ticketObsevationsIndex][1]; let thirdTicketObservation = result[ticketObsevationsIndex][2]; expect(firstTicketObservation.observationTypeFk).toEqual(1); expect(firstTicketObservation.description).toEqual('under the floor'); expect(secondTicketObservation.observationTypeFk).toEqual(2); expect(secondTicketObservation.description).toEqual('wears leather and goes out at night'); expect(thirdTicketObservation.observationTypeFk).toEqual(3); expect(thirdTicketObservation.description).toEqual('care with the dog'); }); it('should confirm the procedure sets companyFk as Tenerife if vProvinceName is SANTA CRUZ DE TENERIFE', async () => { let stmts = []; let stmt; stmts.push('START TRANSACTION'); let params = { clientFk: 101, shipped: today, warehouseFk: 1, companyFk: 442, addressFk: 1, agencyModeFk: 2, routeFk: null, landed: today, userId: 18 }; let tenerife = 1381; stmt = new ParameterizedSQL(`UPDATE province p, client c SET p.name ='SANTA CRUZ DE TENERIFE' WHERE p.id = c.provinceFk AND c.id = ?`, [ params.clientFk, ]); stmts.push(stmt); stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, params.companyFk, params.addressFk, params.agencyModeFk, params.routeFk, params.landed, params.userId ]); stmts.push(stmt); let ticketCompanyIndex = stmts.push(`SELECT companyFk FROM vn.ticket WHERE id = @newTicketId`) - 1; stmts.push('ROLLBACK'); let sql = ParameterizedSQL.join(stmts, ';'); let result = await app.models.Ticket.rawStmt(sql); let ticket = result[ticketCompanyIndex][0]; expect(ticket.companyFk).toEqual(tenerife); }); it('should confirm the procedure sets address if it received it null', async () => { let stmts = []; let stmt; stmts.push('START TRANSACTION'); let params = { clientFk: 101, shipped: today, warehouseFk: 1, companyFk: 442, addressFk: 0, agencyModeFk: 2, routeFk: null, landed: today, userId: 18 }; stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, params.companyFk, params.addressFk, params.agencyModeFk, params.routeFk, params.landed, params.userId ]); stmts.push(stmt); let ticketAddressIndex = stmts.push(`SELECT addressFk FROM vn.ticket WHERE id = @newTicketId`) - 1; stmt = new ParameterizedSQL(`SELECT id FROM vn.address WHERE clientFk = ? AND isDefaultAddress = 1`, [ params.clientFk, ]); let clientDefaultAddressIndex = stmts.push(stmt) - 1; stmts.push('ROLLBACK'); let sql = ParameterizedSQL.join(stmts, ';'); let result = await app.models.Ticket.rawStmt(sql); let ticketAddress = result[ticketAddressIndex][0]; let clientDefaultAddress = result[clientDefaultAddressIndex][0]; expect(ticketAddress.addressFk).toEqual(clientDefaultAddress.id); }); it('should confirm the procedure creates the state as delivered if the client has isCreatedAsServed TRUE', async () => { let stmts = []; let stmt; stmts.push('START TRANSACTION'); let params = { clientFk: 101, shipped: today, warehouseFk: 1, companyFk: 442, addressFk: 0, agencyModeFk: 2, routeFk: null, landed: today, userId: 18 }; stmt = new ParameterizedSQL(`UPDATE vn.client SET isCreatedAsServed = 1 WHERE id = ?`, [ params.clientFk, ]); stmts.push(stmt); stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, params.companyFk, params.addressFk, params.agencyModeFk, params.routeFk, params.landed, params.userId ]); stmts.push(stmt); let ticketStateCodeIndex = stmts.push(`SELECT code FROM vn.ticketState WHERE ticketFk = @newTicketId`) - 1; stmts.push('ROLLBACK'); let sql = ParameterizedSQL.join(stmts, ';'); let result = await app.models.Ticket.rawStmt(sql); let ticketStateCode = result[ticketStateCodeIndex][0].code; expect(ticketStateCode).toEqual('DELIVERED'); }); });