Files
graphql-api-js/src/datasources/order-api.ts
T
2021-04-07 18:30:56 +02:00

148 lines
3.4 KiB
TypeScript

import { SQLDataSource } from 'datasource-sql';
import { Maybe } from './types';
const MINUTE = 60;
interface Address {
id: number;
firstname: string;
}
interface ContactInformation {
email: string;
phone: string;
addressId: number;
}
interface Order {
id: number;
inserted: Date;
paid: boolean;
confirmed: boolean;
delivered: boolean;
newsletter: boolean;
creditInvoice: boolean;
canceled: boolean;
reminder: boolean;
deliveredDate: Date;
countryCode: string;
language: string;
deliveryCountryCode: string;
deliveryMethod: string;
deliveryPrice: string;
deliveryVat: number;
customerType: string;
paymentType: string;
attention: string;
currency: string;
exchangeRate: number;
exchangeRateEur: number;
invoiceId: string;
contractCustomerId: number | null;
vatNumber: string;
comment: string;
cancelsOrderId: number;
cancelledByOrderId: number;
orderlogId: number;
resellerStore: string;
orderSum: number;
klarnaOrderId: string;
pwintyId: number;
email: string;
phone: string;
deliveryEmail: string;
deliveryPhone: string;
customerFirstname: string;
customerLastname: string;
customerEmail: string;
customerCellphone: string;
flyerIds: string;
market: string;
locale: string;
billingInformation: ContactInformation;
deliveryInformation: ContactInformation;
billingAddressId: number;
deliveryAddressId: number;
}
export class OrderAPI extends SQLDataSource {
constructor(config) {
super(config);
}
async getAddress(addressId: number): Promise<Maybe<Address>> {
const row = await this.knex
.select('*')
.from('addresses')
.where('id', addressId)
.first()
.cache(MINUTE);
console.log('HERE', row);
return row
? {
id: row.id,
firstname: row.firstName,
}
: null;
/**
* address = cls()
address.id = row["id"]
address.firstname = row["first_name"]
address.lastname = row["last_name"]
address.companyname = row["company_name"]
address.address1 = row["address1"]
address.address2 = row["address2"]
address.countryCode = row["country_code"]
address.city = row["city"]
address.zipcode = row["zipcode"]
address.stateCountyOrRegion = row["state"]
return address
def resolve_recipientName(self, info):
if self.firstname or self.lastname:
return "{} {}".format(self.firstname, self.lastname).strip()
*/
}
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() {
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);
}
}