{{ message }}
Repository navigation
Expand file tree
/
Copy pathprice-sheet.integration.cjs
More file actions
366 lines (362 loc) · 14.7 KB
/
Copy pathprice-sheet.integration.cjs
File metadata and controls
366 lines (362 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
const { test, before, after, beforeEach } = require('node:test');
const assert = require('node:assert/strict');
const { MongoMemoryServer } = require('mongodb-memory-server');
const { MongoClient, ObjectId } = require('mongodb');
const BaseModel = require('../src/models/base.model');
const service = require('../src/services/price-sheet');
let server, client, database;
const context = {
branchId: new ObjectId().toString(),
licenseId: new ObjectId().toString(),
userName: 'Store owner',
userId: new ObjectId().toString(),
};
const itemId = new ObjectId('123456789012345678901234');
test('rejects inherited field names and retains precise current prices for conflict checks', () => {
assert.throws(
() =>
service.normalize(
{ id: String(itemId), changes: { constructor: { old: 0, value: 1 } } },
true
),
/Unsupported/
);
assert.equal(
service.normalize(
{ id: String(itemId), changes: { selling_price: { old: 28.5714, value: 29 } } },
false
).changes.selling_price.old,
28.5714
);
});
test('invalid raw values appear in reports and recovery sheets include only existing unresolved items', async () => {
const job = await staged([row({ selling_price: { old: 20, value: '=bad' } })]);
const report = await service.sheet(context, { jobId: job });
assert.equal(report.worksheets[0].getRow(2).getCell(6).value, "'=bad");
const retry = await service.sheet(context, { retryJobId: job });
assert.equal(retry.worksheets[0].rowCount, 2);
});
before(async () => {
server = await MongoMemoryServer.create();
client = await MongoClient.connect(server.getUri());
database = client.db('pricing');
BaseModel.getDb = async () => database;
});
after(async () => {
await client?.close();
await server?.stop();
});
beforeEach(async () => {
for (const n of [
'items',
'price_sheet_jobs',
'price_sheet_rows',
'price_history',
'bulk_price_updates',
])
await database.collection(n).deleteMany({});
await database.collection('items').insertOne({
_id: itemId,
branch_id: new ObjectId(context.branchId),
license: new ObjectId(context.licenseId),
name: 'Onion',
itemid: '001',
unit: 'Kg',
selling_price: 20,
company_price: 10,
mrp_price: 30,
available_quantity: 7,
});
});
function row(changes = { selling_price: { old: 20, value: 22 } }) {
return { id: itemId.toString(), name: 'Onion', changes };
}
async function staged(rows, allowZero = false) {
const jobId = new ObjectId().toString();
await service.create(context, { jobId, allowZero });
await service.append(context, jobId, { start: 0, rows });
await service.finish(context, jobId, rows.length);
return jobId;
}
test('updates selected cost/MRP fields without selling-price, stock or sales changes', async () => {
const job = await staged([
row({ company_price: { old: 10, value: 12 }, mrp_price: { old: 30, value: 35 } }),
]);
const result = await service.processBatch(context, job, false);
assert.equal(result.counts.updated, 1);
const it = await database.collection('items').findOne({ _id: itemId });
assert.equal(it.company_price, 12);
assert.equal(it.mrp_price, 35);
assert.equal(it.selling_price, 20);
assert.equal(it.available_quantity, 7);
assert.equal(await database.collection('price_history').countDocuments({}), 2);
assert.equal((await service.processBatch(context, job, false)).counts.updated, 1);
assert.equal(await database.collection('price_history').countDocuments({}), 2);
});
test('blank/zero preservation and explicit zero confirmation', async () => {
assert.deepEqual(
service.normalize(
row({ selling_price: { old: 20, value: '' }, company_price: { old: 10, value: 0 } }),
false
).changes,
{}
);
const job = await staged([row({ selling_price: { old: 20, value: 0 } })], true);
await service.processBatch(context, job, false);
assert.equal((await database.collection('items').findOne({ _id: itemId })).selling_price, 0);
});
test('invalid and duplicate rows do not change prices without explicit skipping', async () => {
const job = await staged([row(), row()]);
assert.equal((await service.status(context, job)).counts.invalid, 2);
await assert.rejects(service.processBatch(context, job, false), /invalid/);
await service.processBatch(context, job, true);
assert.equal((await database.collection('items').findOne({ _id: itemId })).selling_price, 20);
assert.throws(
() => service.normalize(row({ available_quantity: { old: 7, value: 9 } }), false),
/Unsupported/
);
assert.throws(() => service.money('2e4'));
});
test('branch and tenant isolation on jobs, exports and item matching', async () => {
const job = await staged([row()]);
const other = { ...context, branchId: new ObjectId().toString() };
await assert.rejects(service.status(other, job), /not found|unauthorized/);
assert.equal((await service.sheet(other)).worksheets[0].rowCount, 1);
const otherJob = new ObjectId().toString();
await service.create(other, { jobId: otherJob });
await service.append(other, otherJob, { start: 0, rows: [row()] });
assert.equal((await service.finish(other, otherJob, 1)).counts.skipped, 1);
});
test('changes after preview are conflicts and never overwrite new values', async () => {
const job = await staged([row()]);
await database.collection('items').updateOne({ _id: itemId }, { $set: { selling_price: 24 } });
const result = await service.processBatch(context, job, false);
assert.equal(result.counts.conflict, 1);
assert.equal((await database.collection('items').findOne({ _id: itemId })).selling_price, 24);
});
test('recovering after item write/row acknowledgement cannot apply twice', async () => {
const job = await staged([row()]);
await service.processBatch(context, job, false);
await database
.collection('price_sheet_rows')
.updateMany({ job_id: new ObjectId(job) }, { $set: { status: 'pending' } });
await database
.collection('price_sheet_jobs')
.updateOne({ _id: new ObjectId(job) }, { $set: { status: 'running' } });
await database.collection('items').updateOne({ _id: itemId }, { $set: { selling_price: 27 } });
await service.processBatch(context, job, false);
assert.equal((await database.collection('items').findOne({ _id: itemId })).selling_price, 27);
assert.equal(await database.collection('price_history').countDocuments({}), 1);
});
test('XLSX round trip preserves IDs, blank fields and validates branch', async () => {
const wb = await service.sheet(context, { fields: 'selling_price,company_price' }),
ws = wb.worksheets[0];
ws.getRow(2).getCell(ws.getRow(1).values.indexOf('New Selling price')).value = 23;
const file = { originalname: 'prices.xlsx', buffer: await wb.xlsx.writeBuffer() };
const result = await service.importSheet(context, { jobId: new ObjectId().toString() }, file);
assert.equal(result.counts.pending, 1);
assert.equal(result.rows[0].changes.selling_price.value, 23);
assert.equal(result.rows[0].changes.company_price, undefined);
const csv = await service.importSheet(
context,
{ jobId: new ObjectId().toString() },
{ originalname: 'prices.csv', buffer: await wb.csv.writeBuffer() }
);
assert.equal(csv.rows[0].itemId, String(itemId));
assert.equal(csv.counts.pending, 1);
await assert.rejects(
service.importSheet(
{ ...context, branchId: new ObjectId().toString() },
{ jobId: new ObjectId().toString() },
file
),
/different branch|unselected branch/
);
});
test('CSV export filters and result reports do not return whole catalogue', async () => {
assert.equal((await service.sheet(context, { search: 'Missing' })).worksheets[0].rowCount, 1);
const job = await staged([row()]);
const report = await service.sheet(context, { jobId: job });
assert.equal(report.worksheets[0].rowCount, 2);
assert.equal(report.worksheets[0].getRow(1).getCell(1).value, 'Row');
});
test(
'10,000 items stage, validate and update in bounded batches with complete audit counts',
{ timeout: 180000 },
async () => {
const items = Array.from({ length: 10000 }, (_, i) => ({
_id: new ObjectId(),
branch_id: new ObjectId(context.branchId),
license: new ObjectId(context.licenseId),
name: 'Product ' + i,
selling_price: 10,
company_price: 5,
mrp_price: 20,
}));
await database.collection('items').insertMany(items);
const jobId = new ObjectId().toString();
await service.create(context, { jobId });
const rows = items.map((it) => ({
id: String(it._id),
name: it.name,
changes: { selling_price: { old: 10, value: 11 } },
}));
for (let start = 0; start < rows.length; start += 500)
await service.append(context, jobId, { start, rows: rows.slice(start, start + 500) });
assert.equal((await service.finish(context, jobId, 10000)).counts.pending, 10000);
assert.equal((await service.status(context, jobId, 2)).rows[0].number, 51);
let result,
batches = 0;
do {
result = await service.processBatch(context, jobId, false);
assert.ok(result.processed.length <= 100);
batches++;
} while (result.job.status !== 'complete');
assert.equal(batches, 100);
assert.equal(result.counts.updated, 10000);
assert.equal(
await database.collection('price_history').countDocuments({ batch_id: new ObjectId(jobId) }),
10000
);
assert.equal(await database.collection('items').countDocuments({ selling_price: 11 }), 10000);
}
);
test('styled Excel hides trailing IDs and sorted rows still update the correct duplicate-name item', async () => {
const otherId = new ObjectId();
await database.collection('items').insertOne({
_id: otherId,
branch_id: new ObjectId(context.branchId),
license: new ObjectId(context.licenseId),
name: 'Onion',
itemid: '001',
selling_price: 40,
available_quantity: 0,
});
const wb = await service.sheet(context);
const ws = wb.worksheets[0];
assert.deepEqual(ws.getRow(1).values.slice(1), [
'Item name',
'SKU',
'Unit',
'Current Selling price',
'New Selling price',
'Branch ID',
'Item ID',
'Exported at',
'Categories',
]);
assert.equal(ws.getColumn(6).hidden, true);
assert.equal(ws.getColumn(7).hidden, true);
assert.equal(ws.views[0].ySplit, 1);
const row2 = ws.getRow(2).values;
ws.getRow(2).values = ws.getRow(3).values;
ws.getRow(3).values = row2;
const target = [ws.getRow(2), ws.getRow(3)].find((r) => r.getCell(7).value === String(otherId));
target.getCell(5).value = 45;
const file = { originalname: 'sorted.xlsx', buffer: await wb.xlsx.writeBuffer() };
const result = await service.importSheet(context, { jobId: new ObjectId().toString() }, file);
assert.equal(result.counts.pending, 1);
assert.equal(result.counts.unchanged, 1);
assert.equal(result.total, 1);
assert.equal(result.rows[0].itemId, String(otherId));
assert.equal((await service.status(context, result.job._id, 1, '')).total, 2);
await service.processBatch(context, result.job._id, false);
assert.equal((await database.collection('items').findOne({ _id: otherId })).selling_price, 45);
assert.equal(
(await database.collection('items').findOne({ _id: otherId })).available_quantity,
0
);
assert.equal((await database.collection('items').findOne({ _id: itemId })).selling_price, 20);
assert.equal((await service.sheet(context, { jobId: result.job._id })).worksheets[0].rowCount, 2);
});
test('legacy sheets with leading IDs still import and unchanged sheets show no changes', async () => {
const header = 'Branch ID,Item ID,Item name,SKU,Unit,Current Selling price,New Selling price';
const file = {
originalname: 'legacy.csv',
buffer: Buffer.from(
header + '\n' + [context.branchId, String(itemId), 'Onion', '001', 'Kg', 20, 20].join(',')
),
};
const result = await service.importSheet(context, { jobId: new ObjectId().toString() }, file);
assert.equal(result.total, 0);
assert.equal(result.counts.unchanged, 1);
assert.equal((await service.status(context, result.job._id, 1, 'unchanged')).total, 1);
});
test('CSV file import crosses the 500-row staging boundary and reviews only changed prices', async () => {
const items = Array.from({ length: 501 }, (_, i) => ({
_id: new ObjectId(),
branch_id: new ObjectId(context.branchId),
license: new ObjectId(context.licenseId),
name: 'Sheet item ' + i,
selling_price: 10,
}));
await database.collection('items').insertMany(items);
const wb = await service.sheet(context, { search: 'Sheet item' });
const ws = wb.worksheets[0];
ws.getRow(2).getCell(5).value = 12;
ws.getRow(502).getCell(5).value = 13;
const result = await service.importSheet(
context,
{ jobId: new ObjectId().toString() },
{
originalname: '501-items.csv',
buffer: await wb.csv.writeBuffer(),
}
);
assert.equal(result.job.total, 501);
assert.equal(result.counts.pending, 2);
assert.equal(result.counts.unchanged, 499);
assert.equal(result.total, 2);
assert.deepEqual(
result.rows.map((r) => r.number),
[1, 501]
);
assert.equal((await service.processBatch(context, result.job._id, false)).counts.updated, 2);
});
test('multiple categories select the same union for paged editing and sheet export', async () => {
const Repository = require('../src/repositories/item.repository');
const repository = Object.create(Repository.prototype);
repository.getCollection = async () => database.collection('items');
const cats = [new ObjectId(), new ObjectId(), new ObjectId()];
await database.collection('items').deleteMany({});
const base = {
branch_id: new ObjectId(context.branchId),
license: new ObjectId(context.licenseId),
selling_price: 20,
};
await database
.collection('items')
.insertMany(
cats.map((category_id, i) => ({
...base,
_id: new ObjectId(),
name: 'Product ' + i,
category_id,
}))
);
await database
.collection('items')
.insertOne({
...base,
_id: new ObjectId(),
name: 'Other branch',
branch_id: new ObjectId(),
category_id: cats[0],
});
const query = { category: cats.slice(0, 2).join(',') };
const grid = await repository.priceGrid(query, context);
assert.equal(grid.total, 2);
const sheet = await service.sheet(context, query);
assert.equal(sheet.worksheets[0].rowCount, 3);
assert.deepEqual(
sheet.worksheets[0].getColumn(1).values.slice(2),
grid.items.map((i) => i.name)
);
assert.equal((await repository.priceGrid({ category: String(cats[0]) }, context)).total, 1);
assert.equal(
(await repository.priceGrid({ category: query.category, search: 'missing' }, context)).total,
0
);
await assert.rejects(repository.priceGrid({ category: 'invalid' }, context), /Invalid category/);
await assert.rejects(service.sheet(context, { category: 'invalid' }), /Invalid category/);
});
You can’t perform that action at this time.
