Skip to content

Commit

Permalink
fix: addOrderToProcessingQueue params, chore: TS
Browse files Browse the repository at this point in the history
  • Loading branch information
tomek3e committed Nov 21, 2023
1 parent 326620a commit 070ad8b
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 450 deletions.
8 changes: 8 additions & 0 deletions __tests__/orders-dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ test('should save extraData', () => {
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',
Expand Down
138 changes: 0 additions & 138 deletions src/db/orders-dao.js

This file was deleted.

24 changes: 16 additions & 8 deletions src/db/orders-dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'path';
const DB_FILENAME_DEFAULT = './data/orders.db';
const DB_SCHEMA_FILENAME = path.join(__dirname, './schema.sql'); //require.resolve('./schema.sql');

function createDatabase(dbFileName = DB_FILENAME_DEFAULT) {
function createDatabase(dbFileName = DB_FILENAME_DEFAULT): Database {
//console.log(`Working with db file: ${dbFileName}`);
const dir = path.dirname(dbFileName);
if (!fs.existsSync(dir)) {
Expand All @@ -27,11 +27,18 @@ function isOrderInDb(db: Database, orderId: string) {
return row.count1 === 1;
}

function isOrderWithCheckSeqInDb(db: Database, checkSeq: string) {
const row = db
.prepare('SELECT count() as count1 FROM OSL_ORDER WHERE checkSeq=?')
.get([checkSeq]) as any;
return row.count1 === 1;
}

function upsertOrder(db: Database, order: IOrderRecord) {
const addColumnNames = ['extraData', 'orderStatus', 'stage'];
const addColumnNames = ['extraData', 'orderStatus', 'stage', 'checkSeq'];
let additionalColumns = '';
let additionalParams = '';
let vals = [
let vals: any[] = [
order.id,
order.isCreatedCentrally,
order.created,
Expand Down Expand Up @@ -75,7 +82,7 @@ function updateOrderExtraData(
function getOrder(db: Database, orderId: string) {
const stmt = db.prepare('SELECT * FROM OSL_ORDER WHERE id=?');
const result = stmt.get([orderId]);
return result;
return result as IOrderRecord | undefined;
}

function setOrderStage(db: Database, orderId: string, stage: string) {
Expand Down Expand Up @@ -104,10 +111,10 @@ function getOrdersNotDone(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",
);
const orders = [];
const orders: IOrderRecord[] = [];
const cursor = stmt.iterate();
for (const row of cursor) {
orders.push(row);
orders.push(row as IOrderRecord);
}
return orders;
}
Expand All @@ -116,10 +123,10 @@ 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",
);
const orders = [];
const orders: IOrderRecord[] = [];
const cursor = stmt.iterate(stage);
for (const row of cursor) {
orders.push(row);
orders.push(row as IOrderRecord);
}
return orders;
}
Expand Down Expand Up @@ -165,4 +172,5 @@ module.exports = {
setOrderStage,
getOrdersInStage,
getStats,
isOrderWithCheckSeqInDb,
};
2 changes: 1 addition & 1 deletion src/db/task-dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path';
const DB_FILENAME_DEFAULT = './data/task.db';
const DB_SCHEMA_FILENAME = path.join(__dirname, './task.sql'); //require.resolve( './task.sql');

function createDatabase(dbFileName = DB_FILENAME_DEFAULT) {
function createDatabase(dbFileName = DB_FILENAME_DEFAULT): Database {
const db = new BetterSqlLite(dbFileName); //, { verbose: console.log });
const migration = fs.readFileSync(DB_SCHEMA_FILENAME, 'utf8');
db.exec(migration);
Expand Down
Empty file removed src/main.js
Empty file.
131 changes: 0 additions & 131 deletions src/orders-local-queue-service.js

This file was deleted.

Loading

0 comments on commit 070ad8b

Please sign in to comment.