Skip to content

Commit

Permalink
feat(chowly-api-client): send order to chowly ref #2778
Browse files Browse the repository at this point in the history
  • Loading branch information
tudormd committed May 20, 2020
1 parent 26433c0 commit 7b8f33f
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 25 deletions.
40 changes: 31 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,39 @@ The library does not modify request and response payload.
```typescript
import { ChowlyClient } from '@goparrot/chowly-api-client';

(() => {
const client = new ChowlyClient({
apiKey: 'test',
baseUrl: 'baseurl',
maxRetries: 5
});

client.list()
const customer = { /* your customer data */ };

const info = { /* your info data */ };

const item = { /* your item data */ };

const chowlyOrder = {
customer,
info,
items: [item],
};

const client = new ChowlyClient({
apiKey: 'test',
baseUrl: 'baseurl',
maxRetries: 5
});

// Get menu
client.getMenu()
.then(data => console.log(data))
.catch(error => console.log(error));
})();

// Create order
client.createOrder(chowlyOrder)
.then(data => console.log(data))
.catch(error => console.log(error));

// Get order
client.getOrder(orderId)
.then(data => console.log(data))
.catch(error => console.log(error));

```

## Available Options
Expand Down
22 changes: 19 additions & 3 deletions src/client/chowly-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AxiosInstance, AxiosResponse } from 'axios';
import { plainToClass } from 'class-transformer';
import { IClientConfig, ISections } from '../interface';
import { Sections } from '../model';
import { IClientConfig, ISections, IGetOrder, ICreateOrder } from '../interface';
import { Sections, ReadOrder, Order } from '../model';
import { createAxiosInstance } from '../utils';

export class ChowlyApiClient {
Expand All @@ -24,9 +24,25 @@ export class ChowlyApiClient {
});
}

async list(): Promise<Sections> {
async getMenu(): Promise<Sections> {
const response: AxiosResponse<ISections> = await this.client.get<ISections>(`${this.basePrefix}/menus`);

return plainToClass(Sections, response.data);
}

async createOrder(order: ICreateOrder): Promise<Order> {
const response: AxiosResponse<ICreateOrder> = await this.client.post<ICreateOrder>(`${this.basePrefix}/orders`, order, {
headers: { 'Content-Type': 'application/json' },
});

return plainToClass(Order, response.data);
}

async getOrder(orderId: string): Promise<ReadOrder> {
const response: AxiosResponse<IGetOrder> = await this.client.get<IGetOrder>(`${this.basePrefix}/orders/${orderId}`, {
headers: { 'Content-Type': 'application/json' },
});

return plainToClass(ReadOrder, response.data);
}
}
1 change: 1 addition & 0 deletions src/enum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './order.enum';
19 changes: 19 additions & 0 deletions src/enum/order.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export enum DayOfWeekEnum {
MONDAY = 'monday',
TUESDAY = 'tuesday',
WEDNESDAY = 'wednesday',
THURSDAY = 'thursday',
FRIDAY = 'friday',
SATURDAY = 'saturday',
SUNDAY = 'sunday',
}

export enum ServiceTypeEnum {
DELIVERY = 'Delivery',
PICKUP = 'Pick-Up',
}

export enum PaymentTypeEnum {
CASH = 'cash',
CREDIT = 'credit',
}
42 changes: 42 additions & 0 deletions src/interface/get-order.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ICustomer } from '../interface';
import { RetrieveInfo } from '../model';

export interface IPayload {
customer: ICustomer;
info: RetrieveInfo;
items: IRetrieveItem[];
configuration: Record<string, any>;
}

export interface IRetrieveItem {
id: string;
'menu-item-name': string;
quantity: string;
price: string;
'special-instructions': string;
'menu-choices': string;
'menu-choices-hash': IMenuChoice;
'customer-name': string | null;
}

interface IChoice {
name: string;
price: number;
}

export type IMenuChoice = Record<string, IChoice[]>;

export interface IGetOrder {
order: IRetrieveOrder;
}

export interface IRetrieveOrder {
guid: string;
payload: IPayload;
external_id: string;
created_at: string;
completed_at: string | null;
error_at: string | null;
error_string: string | null;
ticket_payload: null;
}
2 changes: 2 additions & 0 deletions src/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './chowly.interface';
export * from './configuration.interface';
export * from './order.interface';
export * from './get-order.interface';
66 changes: 66 additions & 0 deletions src/interface/order.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { DayOfWeekEnum, ServiceTypeEnum, PaymentTypeEnum } from '../enum';
import { ITimePeriod } from './chowly.interface';

export interface ICustomer {
id: string;
name: string;
phone: string;
email: string | null;
address1?: string;
address2: string | null;
city?: string;
state?: string;
zip?: string;
cross_street: string | null;
special_instructions: string | null;
}

export interface IOrderInfo {
id: string;
scheduled_time: IScheduledTime[] | null;
pickup_code: string | null;
service_type: ServiceTypeEnum;
payment_is_cash: boolean;
payment_type: PaymentTypeEnum;
tip_payment_is_cash: boolean;
tip_payment_type: PaymentTypeEnum;
subtotal: string;
delivery_charge: string;
sales_tax: string;
tip: string;
total: string;
coupon_description: string | null;
coupon_amount?: string;
}

export interface IOrderItem {
id: string;
name: string;
external_id: string | null;
price: string;
quantity: string;
notes: string | null;
mods: IMod[];
}

export interface IMod {
id: string;
name: string;
category: string | null;
external_id: string | null;
price: string;
quantity: string;
}

export interface ICreateOrder {
customer: ICustomer;
info: IOrderInfo;
items: IOrderItem[];
id?: string;
}

export interface IScheduledTime {
time_periods: ITimePeriod[];
enabled: boolean;
day_of_week: DayOfWeekEnum;
}
60 changes: 60 additions & 0 deletions src/model/create-order.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { IOrderInfo, ICustomer, IOrderItem, IScheduledTime, IMod, ICreateOrder } from '../interface';
import { ServiceTypeEnum, PaymentTypeEnum } from '../enum';

export class Order implements ICreateOrder {
customer: Customer;
info: Info;
items: ItemOrder[];
id: string;
}

export class Customer implements ICustomer {
id: string;
name: string;
phone: string;
email: string | null;
address1?: string;
address2: string | null;
city?: string;
state?: string;
zip?: string;
cross_street: string | null;
special_instructions: string | null;
}

export class Info implements IOrderInfo {
id: string;
scheduled_time: IScheduledTime[] | null;
pickup_code: string | null;
service_type: ServiceTypeEnum;
payment_is_cash: boolean;
payment_type: PaymentTypeEnum;
tip_payment_is_cash: boolean;
tip_payment_type: PaymentTypeEnum;
subtotal: string;
delivery_charge: string;
sales_tax: string;
tip: string;
total: string;
coupon_description: string | null;
coupon_amount?: string;
}

export class ItemOrder implements IOrderItem {
id: string;
name: string;
external_id: string | null;
price: string;
quantity: string;
notes: string | null;
mods: Mods[];
}

export class Mods implements IMod {
id: string;
name: string;
category: string | null;
external_id: string | null;
price: string;
quantity: string;
}
56 changes: 56 additions & 0 deletions src/model/get-order.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { IGetOrder, IPayload, IRetrieveItem, IRetrieveOrder, IMenuChoice } from '../interface';
import { ServiceTypeEnum, PaymentTypeEnum } from '../enum';
import { Customer } from './create-order.model';

export class ReadOrder implements IGetOrder {
order: RetrieveOrder;
}
class RetrieveOrder implements IRetrieveOrder {
guid: string;
payload: Payload;
external_id: string;
created_at: string;
completed_at: string | null;
error_at: string | null;
error_string: string | null;
ticket_payload: null;
}

export class Payload implements IPayload {
customer: Customer;
info: RetrieveInfo;
items: RetrieveItems[];
configuration: Record<string, any>;
}

export class RetrieveInfo {
'order-id': string;
'special-notes2': string;
'service-type': ServiceTypeEnum;
'payment-is-cash': string;
'tip-payment-is-cash': string;
'payment-type': PaymentTypeEnum;
'tip-payment-type': PaymentTypeEnum;
subtotal: number;
'delivery-charge': number;
'sales-tax': number;
tax_exempt: null;
tip: number;
total: number;
is_future: string;
future_at: string;
placed_at: string;
'coupon-description': string;
'coupon-amount': number;
}

export class RetrieveItems implements IRetrieveItem {
id: string;
'menu-item-name': string;
quantity: string;
price: string;
'special-instructions': string;
'menu-choices': string;
'menu-choices-hash': IMenuChoice;
'customer-name': string | null;
}
2 changes: 2 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './chowly.model';
export * from './create-order.model';
export * from './get-order.model';
Loading

0 comments on commit 7b8f33f

Please sign in to comment.