diff --git a/modules/ticket/back/methods/ticket/specs/itemLackDetail.spec.js b/modules/ticket/back/methods/ticket/specs/itemLackDetail.spec.js index 28da66c4c..9d5a7c6f9 100644 --- a/modules/ticket/back/methods/ticket/specs/itemLackDetail.spec.js +++ b/modules/ticket/back/methods/ticket/specs/itemLackDetail.spec.js @@ -10,7 +10,7 @@ describe('Item Lack Detail', () => { const result = await models.Ticket.itemLackDetail(id, options); - expect(result).toBeFalsy(); + expect(result.length).toEqual(0); await tx.rollback(); } catch (e) { await tx.rollback(); @@ -26,7 +26,7 @@ describe('Item Lack Detail', () => { const id = 1167; const result = await models.Ticket.itemLackDetail(id, options); - expect(result).toBeFalsy(); + expect(result.length).toEqual(0); await tx.rollback(); } catch (e) { await tx.rollback(); @@ -42,7 +42,7 @@ describe('Item Lack Detail', () => { const id = 0; const result = await models.Ticket.itemLackDetail(id, options); - expect(result).toBeFalsy(); + expect(result.length).toEqual(0); await tx.rollback(); } catch (e) { await tx.rollback(); diff --git a/modules/ticket/back/methods/ticket/specs/split.spec.js b/modules/ticket/back/methods/ticket/specs/split.spec.js new file mode 100644 index 000000000..01008b556 --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/split.spec.js @@ -0,0 +1,98 @@ +const models = require('vn-loopback/server/server').models; + +describe('Split', () => { + beforeAll(async() => { + ctx = { + req: { + accessToken: {}, + headers: {origin: 'http://localhost'}, + } + }; + }); + + it('should split tickets with count 1', async() => { + const tx = await models.Ticket.beginTransaction({}); + + const options = {transaction: tx}; + const data = [ + {ticketFk: 7} + ]; + try { + const result = await models.Ticket.split(ctx, data, options); + + expect(result.length).toEqual(1); + expect(result[0].ticket).toEqual(7); + expect(result[0].status).toEqual('noSplit'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should split tickets with count 2 and error', async() => { + const tx = await models.Ticket.beginTransaction({}); + + const options = {transaction: tx}; + const data = [ + {ticketFk: 8} + ]; + try { + const result = await models.Ticket.split(ctx, data, options); + + expect(result.length).toEqual(1); + expect(result[0].ticket).toEqual(8); + expect(result[0].status).toEqual('error'); + expect(result[0].message).toEqual('This ticket is not editable.'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should split tickets with count 2 and other error', async() => { + const tx = await models.Ticket.beginTransaction({}); + + const options = {transaction: tx}; + const data = [ + {ticketFk: 16} + ]; + try { + const result = await models.Ticket.split(ctx, data, options); + + expect(result.length).toEqual(1); + expect(result[0].ticket).toEqual(16); + expect(result[0].status).toEqual('error'); + expect(result[0].message).toEqual('Can\'t transfer claimed sales'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should split tickets with count 2 and success', async() => { + const tx = await models.Ticket.beginTransaction({}); + + const options = {transaction: tx}; + const data = [ + {ticketFk: 32} + ]; + try { + const result = await models.Ticket.split(ctx, data, options); + + expect(result.length).toEqual(1); + expect(result[0].ticket).toEqual(32); + expect(result[0].status).toEqual('split'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/ticket/back/methods/ticket/split.js b/modules/ticket/back/methods/ticket/split.js index 8772bad87..54ba2123a 100644 --- a/modules/ticket/back/methods/ticket/split.js +++ b/modules/ticket/back/methods/ticket/split.js @@ -1,6 +1,3 @@ -const {ParameterizedSQL} = require('loopback-connector/lib/sql'); -const UserError = require('vn-loopback/util/user-error'); - module.exports = Self => { Self.remoteMethodCtx('split', { description: 'Split a ticket or n tickets', @@ -24,7 +21,7 @@ module.exports = Self => { }); Self.split = async(ctx, tickets, options) => { - // const models = Self.app.models; + const models = Self.app.models; const myOptions = {}; let tx; let results = []; @@ -46,15 +43,11 @@ module.exports = Self => { ON s.ticketFk = t.id WHERE t.id IN (?) GROUP BY t.id;`, [ticketsIds], myOptions); - console.log(ticketsCount); - // stmts.push(stmt); - // const sql = ParameterizedSQL.join(stmts, ';'); - // const result = await conn.executeStmt(sql, myOptions); - for (const {tid, sid, count} of ticketsCount) { + for (const {tid, count} of ticketsCount) { try { if (count === 1) { - results.push({ticket: tid, message: 'noSplit'}); + results.push({ticket: tid, status: 'noSplit'}); continue; } const [, [{vNewTicket}]] = await Self.rawSql(` @@ -63,15 +56,20 @@ module.exports = Self => { [tid], myOptions); if (vNewTicket === 0) continue; - await Self.rawSql(` + const sales = await models.Sale.find({ + where: {ticketFk: tid} + }, myOptions); + + const updateIsPicked = sales.map(({sid}) => Self.rawSql(` UPDATE vn.sale SET isPicked = (id = ?) WHERE ticketFk = ?`, - [sid, tid], myOptions); - await Self.transferSales(ctx, tid, vNewTicket, sid, myOptions); + [sid, tid], myOptions)); + await Promise.all(updateIsPicked); + await Self.transferSales(ctx, tid, vNewTicket, sales, myOptions); await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [tid, 'FIXING'], myOptions); - results.push({ticket: tid, message: 'split'}); - } catch (error) { - throw new UserError('You cannot close tickets for today'); + results.push({ticket: tid, status: 'split'}); + } catch ({message}) { + results.push({ticket: tid, status: 'error', message}); } } return results;