Files
graphql-api-js/src/datasources/order-api.ts
T

190 lines
4.5 KiB
TypeScript

import { BaseSQLDataSource } from './BaseSQLDataSource';
import { camelcase } from 'stringcase';
import {
Address,
ContactInformation,
Market,
Order,
OrderRow,
} from './order-types';
import { Maybe } from './types';
const MINUTE = 60;
export class OrderAPI extends BaseSQLDataSource {
cache: any;
constructor(config) {
super(config);
}
async getMarketById(id: number): Promise<Market> {
return await this.knex
.select('*')
.from('markets')
.where('id', id)
.first()
.cache(MINUTE);
}
async getMarketByName(name: string): Promise<Market> {
return await this.knex
.select('*')
.from('markets')
.where('name', name)
.first()
.cache(MINUTE);
}
async getAddress(addressId: number): Promise<Maybe<Address>> {
const row = await this.knex
.select('*')
.from('addresses')
.where('id', addressId)
.first()
.cache(MINUTE);
return row
? {
...row,
/* Change this later to camelCase but also need to change in Pwinty lambda */
firstname: row.firstName,
lastname: row.lastName,
companyname: row.companyName,
recipientName: `${row.firstName} ${row.lastName}`.trim(),
stateCountyOrRegion: row.state ? row.state : null,
}
: null;
}
getBillingInformation(row): ContactInformation {
return {
email: 'niklas.fondberg@photowall.se',
phone: '+46761386397',
addressId: row.billingAddressId,
};
}
getDeliveryInformation(row): ContactInformation {
return {
email: 'niklas.fondberg@photowall.se',
phone: '+46761386397',
addressId: row.deliveryAddressId,
};
}
async getOrders(): Promise<Array<Order>> {
return await this.knex
.select('*')
.from('orders')
.where('inserted', '>=', '2021-03-01T00:00:00Z')
.cache(MINUTE)
.then((rows) =>
rows.map((row) => {
row.billingInformation = this.getBillingInformation(row);
row.deliveryInformation = this.getDeliveryInformation(row);
return row;
}),
);
}
async getOrder(id: number): Promise<Order> {
return await this.knex
.select('*')
.from('orders')
.where('id', id)
.first()
.cache(MINUTE)
.then((row) => {
row.billingInformation = this.getBillingInformation(row);
row.deliveryInformation = this.getDeliveryInformation(row);
return row;
});
}
async getOrderRows(orderId: number): Promise<Array<OrderRow>> {
const fieldsRes = await this.cacheQuery(
120,
this.knex
.raw('SELECT * from "order-row_fields"')
.then((data) => data.rows),
);
const fields = fieldsRes.reduce((obj, item) => {
obj[item.fieldid] = item.name;
return obj;
}, {});
const rows = await this.knex
.raw('SELECT * FROM "order-rows" WHERE orderid = ?', orderId)
.then((data) => data.rows);
let orderRows = [];
for (let i = 0; i < rows.length; i++) {
const rowId = rows[i].rowid;
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) {
continue;
}
const orderRow = details.reduce((obj, item) => {
obj[camelcase(fields[item.fieldid])] = item.value;
return obj;
}, {});
if ('group' in orderRow) {
orderRow['productGroup'] = orderRow['group'];
}
orderRow['id'] = rowId;
orderRow['orderId'] = orderId;
orderRows.push(orderRow);
}
return orderRows;
}
}
/**
* row
{
printId: '73963',
inserted: 2021-02-04T10:11:41.093Z,
productId: '54192',
group: 'photo-wallpaper',
type: 'scaling',
artNo: 'e50075',
path: 'flora-hysterica-4',
name: 'Flora Hysterica 4',
price: '587.092480',
width: '640',
height: '260',
x: '0.000000',
y: '0.135133',
weight: '1',
designer: 'martin-bergstrom',
designerId: '201',
commission: '0',
commissionResale: '0',
status: 'print',
process: 'true',
vat: '1.210000',
commissionAmount: '0.000000',
framed: '0',
mirrored: '0',
edge: '0',
imageprocessed: 'true',
imageprocessingrejected: 'false',
resolution: '150',
imagedonotprint: 'false',
material: 'premium-wallpaper',
discountType: '%',
discountValue: '25',
measureUnit: 'cm',
displayWidth: '640',
displayHeight: '260',
designerPath: 'martin-bergstrom'
},
*/