Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: process/retrieve all orders not in stage DONE #4

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions __tests__/orders-dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ test('upsert order and check is it is added', () => {
expect(orderDao.isOrderInDb(db, order1.id)).toBe(true);
orderDao.upsertOrder(db, order2);
expect(orderDao.isOrderInDb(db, order2.id)).toBe(true);
orderDao.upsertOrder(db, orderAbandoned);
expect(orderDao.isOrderInDb(db, orderAbandoned.id)).toBe(true);
orderDao.upsertOrder(db, orderClosed);
expect(orderDao.isOrderInDb(db, orderClosed.id)).toBe(true);
});

test('retrieves CLOSED and ABANDONED orders for processing if stage !== DONE', async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
const orders = orderDao.getOrdersToProcess(db);
expect(orders.some((o) => o.orderStatus === 'ABANDONED')).toBe(true);
expect(orders.some((o) => o.orderStatus === 'CLOSED')).toBe(true);
}, 4000);

test('check stats', () => {
const stats = orderDao.getStats(db);
expect(stats.totalOrders).toBe(2);
expect(stats.totalOrders).toBe(4);
});

test('retrieve order from db', () => {
Expand All @@ -27,13 +38,13 @@ test('retrieve order from db', () => {

test('get order by stage, check order and condition', () => {
const orders = orderDao.getOrdersInStage(db, 'SECOND');
expect(orders.length).toBe(1);
expect(orders.length).toBe(3);
orderDao.setOrderStage(db, order1.id, 'SECOND');
const orders2 = orderDao.getOrdersInStage(db, 'SECOND');
expect(orders2.length).toBe(2);
expect(orders2.length).toBe(4);
orderDao.setOrderStage(db, order1.id, 'DONE');
const orders3 = orderDao.getOrdersInStage(db, 'SECOND');
expect(orders3.length).toBe(1);
expect(orders3.length).toBe(3);
});

// test('setOrderProcessedLocally', () => {
Expand Down Expand Up @@ -110,7 +121,7 @@ test('remove closed orders', () => {
orderDao.upsertOrder(db, orderA);
expect(orderDao.getStats(db).totalOrders).toBe(initialTotalOrders + 1);
orderDao.removeClosedOrdersOrAbandoned(db);
expect(orderDao.getStats(db).totalOrders).toBe(initialTotalOrders);
expect(orderDao.getStats(db).totalOrders).toBe(initialTotalOrders - 2);
});

test('should save extraData', () => {
Expand Down Expand Up @@ -157,3 +168,27 @@ const order2 = {
isCreatedCentrally: 1,
stage: 'SECOND',
};

const orderAbandoned = {
id: '411fe885-4980-4ecf-bbb2-5a3f28506c65',
created: new Date().toISOString(),
orderStatus: 'ABANDONED',
orderbody: JSON.stringify({
id: '411fe885-4980-4ecf-bbb2-5a3f28506c65',
total: 123.12,
}),
isCreatedCentrally: 1,
stage: 'SECOND',
};

const orderClosed = {
id: '4f2cda27-427c-48b3-a2ad-19a30d57b329',
created: new Date().toISOString(),
orderStatus: 'CLOSED',
orderbody: JSON.stringify({
id: '4f2cda27-427c-48b3-a2ad-19a30d57b329',
total: 123.12,
}),
isCreatedCentrally: 1,
stage: 'SECOND',
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orderingstack/pos-integrator-core",
"version": "2.1.0",
"version": "2.1.1",
"description": "Ordering Stack POS integrator core - library for easy developing POS integrations with Ordering Stack platform.",
"scripts": {
"test": "jest",
Expand Down
6 changes: 3 additions & 3 deletions src/db/orders-dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function removeClosedOrdersOrAbandoned(db: Database) {

function getOrdersToProcess(db: Database) {
const stmt = db.prepare(
"SELECT * FROM OSL_ORDER WHERE stage<>'DONE' AND orderStatus<>'CLOSED' AND orderStatus<>'ABANDONED' AND nextStageRunAt<CURRENT_TIMESTAMP ORDER BY created DESC",
"SELECT * FROM OSL_ORDER WHERE stage<>'DONE' AND nextStageRunAt<CURRENT_TIMESTAMP ORDER BY created DESC",
);
const orders: IOrderRecord[] = [];
const cursor = stmt.iterate();
Expand All @@ -127,7 +127,7 @@ function getOrdersToProcess(db: Database) {

function getOpenOrders(db: Database) {
const stmt = db.prepare(
"SELECT * FROM OSL_ORDER WHERE stage<>'DONE' AND orderStatus<>'CLOSED' AND orderStatus<>'ABANDONED' ORDER BY created DESC",
"SELECT * FROM OSL_ORDER WHERE stage<>'DONE' ORDER BY created DESC",
);
const orders: IOrderRecord[] = [];
const cursor = stmt.iterate();
Expand All @@ -139,7 +139,7 @@ function getOpenOrders(db: Database) {

function getOrdersInStage(db: Database, stage: string) {
const stmt = db.prepare(
"SELECT * FROM OSL_ORDER WHERE stage=? AND orderStatus<>'CLOSED' AND orderStatus<>'ABANDONED' ORDER BY created DESC",
'SELECT * FROM OSL_ORDER WHERE stage=? ORDER BY created DESC',
);
const orders: IOrderRecord[] = [];
const cursor = stmt.iterate(stage);
Expand Down
Loading