Adapt for new ordermodel (#97)

Co-authored-by: Niklas Fondberg <niklas.fondberg@photowall.se>
This commit is contained in:
Fredrik Ringqvist
2022-01-20 07:11:43 +01:00
committed by GitHub
co-authored by Niklas Fondberg
parent ea086568d0
commit e57cab3b23
22 changed files with 467 additions and 2842 deletions
+32 -133
View File
@@ -1,14 +1,6 @@
import { BaseSQLDataSource } from './BaseSQLDataSource';
import { camelcase } from 'stringcase';
import {
Address,
ContactInformation,
Order,
OrderRow,
} from '../types/order-types';
import { DateFilterInput, GeneralInput, Maybe } from '../types/types';
import { convertToPossibleType } from './utils';
import { getProductGroupStringFromString } from '../types/product-types';
import { Address, Order, OrderRow } from '../types/order-types';
import { GeneralInput, Maybe } from '../types/types';
const MINUTE = 60;
export class OrderAPI extends BaseSQLDataSource {
@@ -37,29 +29,6 @@ export class OrderAPI extends BaseSQLDataSource {
: null;
}
getBillingInformation(row): ContactInformation {
return {
email: row.email,
phone: row.phone,
addressId: row.billingAddressId,
};
}
getDeliveryInformation(row): ContactInformation {
return {
email: row.deliveryEmail,
phone: row.deliveryPhone,
addressId: row.deliveryAddressId,
};
}
createOrderFromRow(row: any): Order {
row.__resolveType = 'Order';
row.billingInformation = this.getBillingInformation(row);
row.deliveryInformation = this.getDeliveryInformation(row);
return row;
}
async getOrdersTotal(input: Maybe<GeneralInput>): Promise<number> {
const res = await this.getOrdersQuery(input)
.clone()
@@ -74,9 +43,7 @@ export class OrderAPI extends BaseSQLDataSource {
// TODO: handle offset
query = query.orderBy('inserted', 'ASC');
return query
.cache(MINUTE)
.then((rows) => rows.map((row) => this.createOrderFromRow(row)));
return query.cache(MINUTE);
}
private getOrdersQuery(input: GeneralInput) {
@@ -93,13 +60,7 @@ export class OrderAPI extends BaseSQLDataSource {
}
async getOrderById(id: number): Promise<Order> {
return this.knex
.select('*')
.from('orders')
.where('id', id)
.first()
.cache(MINUTE)
.then((row) => this.createOrderFromRow(row));
return this.knex.select('*').from('orders').where('id', id).first();
}
async getOrderByExternalId(
@@ -117,111 +78,49 @@ export class OrderAPI extends BaseSQLDataSource {
.where(column, value)
.orderBy('id')
.first()
.cache(MINUTE)
.then((row) => {
return row ? this.createOrderFromRow(row) : null;
});
}
async getOrderRowFieldMapping(): Promise<any> {
// const fields = await this.knex.select('*').from('order-row_fields'); doesn't work becuase of string case...
const fieldsRes = await this.cachedRaw('SELECT * from "order-row_fields"')
.cache(MINUTE * 60)
.then((data) => data.rows);
return fieldsRes.reduce((obj, item) => {
obj[item.fieldid] = item.name;
return obj;
}, {});
}
/**
* Gets the row details
*/
async getOrderRowDetailsForRowId(rowId: number): Promise<any> {
const details = await this.knex
.raw('SELECT * FROM "order-rows_details" WHERE rowid = ?', rowId)
.then((data) => data.rows);
// Skip empty order rows
if (!details.length) {
return null;
}
const fields = await this.getOrderRowFieldMapping();
const orderRow = details.reduce((obj, item) => {
const value = convertToPossibleType(item.value);
obj[camelcase(fields[item.fieldid])] = value;
return obj;
}, {});
if ('group' in orderRow) {
orderRow['productGroup'] = orderRow['group'];
orderRow['group'] = getProductGroupStringFromString(orderRow['group']);
}
return orderRow;
}
/**
* Takes input from order-rows query and gets the row details
*/
async getOrderRows(rows: any): Promise<Array<OrderRow>> {
let orderRows = [];
for (let i = 0; i < rows.length; i++) {
const rowId = rows[i].rowid;
const orderRow = await this.getOrderRowDetailsForRowId(rowId);
if (!orderRow) {
continue;
}
// take some data from order-rows results
orderRow['id'] = rowId;
orderRow['orderId'] = rows[i].orderid;
orderRow['inserted'] = rows[i].inserted;
orderRows.push(orderRow);
}
return orderRows;
.cache(MINUTE);
}
async getOrderRowsByOrderId(orderId: number): Promise<Array<OrderRow>> {
const rows = await this.knex
.raw('SELECT * FROM "order-rows" WHERE orderid = ?', orderId)
.then((data) => data.rows);
return this.getOrderRows(rows);
return this.knex
.select('*')
.from('order_rows')
.where('order_id', orderId)
.then((rows) => this.createOrderRows(rows));
}
async getOrderRowsByDesignerId(
designerId: number,
input: Maybe<GeneralInput>,
): Promise<Array<OrderRow>> {
const query = /* sql */ `
SELECT orderdetails.*, "order-rows".orderid
FROM "order-rows_details" orderdetails
JOIN "order-rows" ON "order-rows".rowid = orderdetails.rowid
WHERE orderdetails.fieldid = 105
AND orderdetails.value = ?
AND orderdetails.inserted >= ?
AND orderdetails.inserted <= ?
ORDER BY orderdetails.inserted ASC
${input?.pagination.limit ? `LIMIT ${input.pagination.limit}` : ''}
`;
const from = input?.filter?.dates?.from
? input.filter.dates.from
: new Date('2000-01-01');
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const to = input?.filter?.dates?.to ? input.filter?.dates.to : tomorrow; // Tomorrow
const limit = input?.pagination.limit ?? 50000;
const rows = await this.knex
.raw(query, [designerId, this.getSQLDate(from), this.getSQLDate(to)])
.then((data) => data.rows);
return this.getOrderRows(rows);
return this.knex
.select('*')
.from('order_rows')
.where('designer_id', designerId)
.whereBetween('inserted', [this.getSQLDate(from), this.getSQLDate(to)])
.orderBy('inserted', 'desc')
.limit(limit)
.then((rows) => this.createOrderRows(rows));
}
createOrderRows(rows: Array<any>): Array<OrderRow> {
return rows.map((row) => {
return {
...row,
data: {
...row.data,
pwintySku: row.data.pwinty_sku ?? null,
pwintyImageId: row.data.pwinty_image_id ?? null,
frameColor: row.data.frame_color ?? null,
},
};
});
}
}