Skip to content

Commit

Permalink
Merge pull request #2 from orderingstack/feat/extraData-typescript-pr…
Browse files Browse the repository at this point in the history
…ettier

Feat/extra data typescript prettier
  • Loading branch information
lniepala authored Nov 28, 2023
2 parents 2079114 + 9747591 commit 0deb93e
Show file tree
Hide file tree
Showing 29 changed files with 3,785 additions and 1,887 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x]

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/build

# misc
.idea
.DS_Store
.env
.env.local
Expand Down
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2023-11-21
- function updateOrderExtraData
- axios version bump
- migrate to typescript

## [1.3.0] - 2023-06-05

- feat: add checkSeq column
Expand Down
194 changes: 105 additions & 89 deletions __tests__/orders-dao.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
const orderDao = require('../src/db/orders-dao');
let db = null;
test('create db from migration', () => {
db = orderDao.createDatabase(':memory:');
db = orderDao.createDatabase(':memory:');
});

test('upsert order and check is it is added', () => {
orderDao.upsertOrder(db, order1);
expect(orderDao.isOrderInDb(db, order1.id)).toBe(true);
orderDao.upsertOrder(db, order2);
expect(orderDao.isOrderInDb(db, order2.id)).toBe(true);
orderDao.upsertOrder(db, order1);
expect(orderDao.isOrderInDb(db, order1.id)).toBe(true);
orderDao.upsertOrder(db, order2);
expect(orderDao.isOrderInDb(db, order2.id)).toBe(true);
});

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


test('retrieve order from db', () => {
const order = orderDao.getOrder(db, order1.id);
expect(order1.orderbody).toBe(order.orderbody);
const order = orderDao.getOrder(db, order1.id);
expect(order1.orderbody).toBe(order.orderbody);
});

// test('remove orders older than x days', () => {
// orderDao.removeOlderThan(db, 2);
// });

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

// test('setOrderProcessedLocally', () => {
Expand All @@ -51,93 +50,110 @@ test('get order by stage, check order and condition', () => {
// expect(orderAfter2.processedLocallyAt).not.toBeNull();
// });


test('upsert order with additional columns', () => {
const orderA = {
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18900',
created: '2020-12-12',
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18900',
total: 1.34,
}),
isCreatedCentrally: 1,
stage: 'FIRST',
}

orderDao.upsertOrder(db, orderA);
expect(orderDao.getOrder(db, orderA.id).stage).toBe('FIRST');
const orderA = {
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18900',
created: '2020-12-12',
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18900',
total: 1.34,
}),
isCreatedCentrally: 1,
stage: 'FIRST',
};

orderDao.upsertOrder(db, orderA);
expect(orderDao.getOrder(db, orderA.id).stage).toBe('FIRST');
});

test('is created centrally field', () => {
const orderL = {
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18901',
created: '2020-12-13',
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18901',
total: 4.56,
}),
isCreatedCentrally: 0,
stage: 'STAGEX',
}
orderDao.upsertOrder(db, orderL);
expect(orderDao.getOrder(db, orderL.id).isCreatedCentrally).toBe(0);
const orderL = {
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18901',
created: '2020-12-13',
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d18901',
total: 4.56,
}),
isCreatedCentrally: 0,
stage: 'STAGEX',
};
orderDao.upsertOrder(db, orderL);
expect(orderDao.getOrder(db, orderL.id).isCreatedCentrally).toBe(0);
});

test('update order body', () => {
const ob = JSON.parse(order1.orderbody);
order1.orderbody = JSON.stringify({
...ob,
newField: '123'
});
orderDao.updateOrderBody(db, order1);
const retrievedOrder = orderDao.getOrder(db, order1.id);
expect(retrievedOrder.orderbody).toBe(order1.orderbody);
const ob = JSON.parse(order1.orderbody);
order1.orderbody = JSON.stringify({
...ob,
newField: '123',
});
orderDao.updateOrderBody(db, order1);
const retrievedOrder = orderDao.getOrder(db, order1.id);
expect(retrievedOrder.orderbody).toBe(order1.orderbody);
});

test('remove closed orders', () => {
const initialTotalOrders = orderDao.getStats(db).totalOrders;
const orderA = {
id: 'bfe47a95-95fd-419b-994d-4e61ca42c356',
created: '2020-12-12',
orderbody: JSON.stringify({
id: 'bfe47a95-95fd-419b-994d-4e61ca42c356',
total: 1.34,
}),
orderStatus: 'CLOSED',
stage: 'DONE',
}

orderDao.upsertOrder(db, orderA);
expect(orderDao.getStats(db).totalOrders).toBe(initialTotalOrders+1);
orderDao.removeClosedOrdersOrAbandoned(db);
expect(orderDao.getStats(db).totalOrders).toBe(initialTotalOrders);
const initialTotalOrders = orderDao.getStats(db).totalOrders;
const orderA = {
id: 'bfe47a95-95fd-419b-994d-4e61ca42c356',
created: '2020-12-12',
orderbody: JSON.stringify({
id: 'bfe47a95-95fd-419b-994d-4e61ca42c356',
total: 1.34,
}),
orderStatus: 'CLOSED',
stage: 'DONE',
};

orderDao.upsertOrder(db, orderA);
expect(orderDao.getStats(db).totalOrders).toBe(initialTotalOrders + 1);
orderDao.removeClosedOrdersOrAbandoned(db);
expect(orderDao.getStats(db).totalOrders).toBe(initialTotalOrders);
});

test('should save extraData', () => {
orderDao.upsertOrder(db, order1);
orderDao.updateOrderExtraData(
db,
order1.id,
JSON.stringify({ key: 'value' }),
);
const order = orderDao.getOrder(db, order1.id);
expect(order.extraData).toBe('{"key":"value"}');
});

test('should save correct types', () => {
const noOrder = orderDao.getOrder(db, 'non-existent-id');
const order = orderDao.getOrder(db, order1.id);
expect(typeof noOrder).toBe('undefined');
expect(typeof order).toBe('object');
expect(typeof order.isCreatedCentrally).toBe('number');
});

const order1 = {
id: 'a296192d-1850-4c2f-8aea-76f859fd682e',
created: '2020-12-07',
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'a296192d-1850-4c2f-8aea-76f859fd682e',
created: '2020-12-07',
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'a296192d-1850-4c2f-8aea-76f859fd682e',
total: 123.12,
}),
isCreatedCentrally: 1,
stage: 'FIRST',
}
total: 123.12,
}),
isCreatedCentrally: 1,
stage: 'FIRST',
};

var oldDate = new Date();
oldDate.setDate(oldDate.getDate() - 5);
const order2 = {
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d189dc',
created: oldDate.toISOString(),
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d189dc',
created: oldDate.toISOString(),
orderStatus: 'NEW',
orderbody: JSON.stringify({
id: 'f53d99fc-63e0-4a51-a9cd-0d3706d189dc',
total: 99.99,
}),
isCreatedCentrally: 1,
stage: 'SECOND',
}

total: 99.99,
}),
isCreatedCentrally: 1,
stage: 'SECOND',
};
Loading

0 comments on commit 0deb93e

Please sign in to comment.