fix: refs #7301 update SQL fixtures and improve lastEntriesFilter logic #3324

Merged
pablone merged 3 commits from 7301-removeRedundantInventories into dev 2025-01-02 09:54:10 +00:00
2 changed files with 40 additions and 14 deletions
Showing only changes of commit ea4b118015 - Show all commits

View File

@ -33,6 +33,7 @@ module.exports = Self => {
w.id warehouseFk, w.id warehouseFk,
w.name warehouse, w.name warehouse,
CAST(tr.landed AS CHAR) landed, CAST(tr.landed AS CHAR) landed,
tr.landed landedDate,
b.id buyFk, b.id buyFk,
b.entryFk, b.entryFk,
b.isIgnored, b.isIgnored,

View File

@ -1,17 +1,22 @@
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
const itemFk = 1; const itemFk = 1;
const today = Date.vnNew();
today.setHours(23, 59, 59, 999);
const twoMonthsAgo = Date.vnNew();
twoMonthsAgo.setHours(0, 0, 0, 0);
twoMonthsAgo.setMonth(twoMonthsAgo.getMonth() - 2, 1);
describe('item lastEntriesFilter()', () => { describe('item lastEntriesFilter()', () => {
it('should return two entry for the given item', async() => { it('should return two entry for the given item', async() => {
const minDate = Date.vnNew(); const minDate = Date.vnNew();
minDate.setHours(0, 0, 0, 0); minDate.setHours(0, 0, 0, 0);
const maxDate = Date.vnNew();
maxDate.setHours(23, 59, 59, 999);
const tx = await models.Item.beginTransaction({}); const tx = await models.Item.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
try { try {
const filter = {where: {itemFk, landed: {between: [minDate, maxDate]}}}; const filter = {where: {itemFk, landed: {between: [minDate, today]}}};
const result = await models.Item.lastEntriesFilter(filter, options); const result = await models.Item.lastEntriesFilter(filter, options);
expect(result.length).toEqual(2); expect(result.length).toEqual(2);
@ -24,21 +29,14 @@ describe('item lastEntriesFilter()', () => {
}); });
it('should return six entries for the given item', async() => { it('should return six entries for the given item', async() => {
const minDate = Date.vnNew();
minDate.setHours(0, 0, 0, 0);
minDate.setMonth(minDate.getMonth() - 2, 1);
const maxDate = Date.vnNew();
maxDate.setHours(23, 59, 59, 59);
const tx = await models.Item.beginTransaction({}); const tx = await models.Item.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
try { try {
const filter = {where: {itemFk, landed: {between: [minDate, maxDate]}}}; const filter = {where: {itemFk, landed: {between: [twoMonthsAgo, today]}}};
const result = await models.Item.lastEntriesFilter(filter, options); const result = await models.Item.lastEntriesFilter(filter, options);
const minDateUtc = minDate.getTime(); const twoMonthsAgoUtc = twoMonthsAgo.getTime();
const maxDateUtc = maxDate.getTime(); const todayUtc = today.getTime();
const resultMatch = ( const resultMatch = (
await Promise.all( await Promise.all(
@ -51,7 +49,7 @@ describe('item lastEntriesFilter()', () => {
const isItemFkValid = itemRecord?.id === itemFk; const isItemFkValid = itemRecord?.id === itemFk;
const landedDate = Date.vnNew(item.landed).getTime(); const landedDate = Date.vnNew(item.landed).getTime();
const isLandedValid = landedDate >= minDateUtc && landedDate <= maxDateUtc; const isLandedValid = landedDate >= twoMonthsAgoUtc && landedDate <= todayUtc;
return isItemFkValid && isLandedValid; return isItemFkValid && isLandedValid;
}) })
@ -66,4 +64,31 @@ describe('item lastEntriesFilter()', () => {
throw e; throw e;
} }
}); });
it('should return just the inventoried inventory', async() => {
const tx = await models.Item.beginTransaction({});
const options = {transaction: tx};
try {
const filter = {where: {itemFk, landed: {between: [twoMonthsAgo, today]}}};
const result = await models.Item.lastEntriesFilter(filter, options);
const {supplierFk} = await models.InventoryConfig.findOne(options);
const {inventoried} = await models.Config.findOne(options);
let hasInventoriedDate = false;
result.forEach(entry => {
if (entry.supplierFk === supplierFk &&
entry.landedDate.getTime() === inventoried.getTime()
)hasInventoriedDate = true;
});
expect(hasInventoriedDate).toEqual(true);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
}); });