Skip to content

Commit

Permalink
Payment & PaymentMethod scenarios, #15, #21
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Jun 18, 2023
1 parent d18fad0 commit 7c05006
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ DISCORD_REDIRECT_URL=http://localhost:3000/api/authentication/discord/callback
DISCORD_BOT_TOKEN=
DISCORD_BOT_PERMISSIONS=65983381777601
DISCORD_SERVER_ID=
DEFAULT_TIMEZONE=Pacific/Auckland
DEFAULT_TIMEZONE=Pacific/Auckland
IS_LOCAL=
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ export interface Location extends LocationData {
updatedAt: string;
}

export type MaybeNumberString = `${number}` | number | string;

export interface OfferPrice {
price: MaybeNumberString;
currency: string;
}

export interface ProductOfferItem {
type: "product";
productId: string;
Expand All @@ -389,9 +396,8 @@ export type OfferStatus =
| "soldOut"
| "void"

export type NumberString = `${number}` | number;

export interface OfferData extends Record<string, unknown> {
export interface OfferData extends Record<string, unknown>, Partial<OfferPrice> {
status: OfferStatus;
items: OfferItem[];
// The user that is providing this offer
Expand All @@ -401,8 +407,6 @@ export interface OfferData extends Record<string, unknown> {
offerName?: string;
// Is the offer publicly visible
public?: boolean;
price?: NumberString;
currency?: string;
countryCode?: string;
}

Expand All @@ -420,6 +424,8 @@ export interface OrderData {
items?: (OrderItemIdentifierData & Partial<OrderItem>)[];
to?: ShipmentTo;
from?: ShipmentFrom; // Is it from a specific known location?
paymentId?: string;
paymentMethodId?: string;
}

export interface Order extends OrderData {
Expand Down Expand Up @@ -511,6 +517,8 @@ export interface PaymentData extends Record<string, unknown> {
status: PaymentStatus;
paymentMethodId: string;
reference?: string;
userId?: string;
organisationId?: string;
}

export interface Payment extends PaymentData {
Expand All @@ -528,6 +536,8 @@ export type PaymentMethodStatus = "pending" | "available" | "expired" | "void";
export interface PaymentMethodData extends Record<string, unknown> {
status: PaymentMethodStatus;
type: PaymentMethodType;
userId?: string;
organisationId?: string;
}

export interface PaymentMethod extends PaymentMethodData {
Expand Down Expand Up @@ -560,10 +570,11 @@ export interface Product extends ProductData {
export type ShipmentStatus = "pending" | "processing" | "sent" | "delivered";

export interface ShipmentLocation {
organisationId?: string; // Optional fixed organisation
locationId?: string; // Optional fixed location
inventoryId?: string; // Optional fixed inventory set
inventoryItemId?: string; // Optional fixed inventory set
userId?: string;
organisationId?: string;
locationId?: string;
inventoryId?: string;
inventoryItemId?: string;
address?: string[]; // Human-readable address
countryCode?: string;
}
Expand Down
3 changes: 3 additions & 0 deletions src/data/offer/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const {
DEFAULT_CURRENCY = "NZD"
} = process.env
1 change: 1 addition & 0 deletions src/data/offer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./list-offers";
export * from "./add-offer";
export * from "./set-offer";
export * as offerSchema from "./schema";
export * from "./env";
13 changes: 9 additions & 4 deletions src/data/offer/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {Identifier} from "../identifier";


export type MaybeNumberString = `${number}` | number | string;

export interface OfferPrice {
price: MaybeNumberString;
currency: string;
}

export interface ProductOfferItem {
type: "product";
productId: string;
Expand All @@ -23,9 +31,8 @@ export type OfferStatus =
| "soldOut"
| "void"

export type NumberString = `${number}` | number;

export interface OfferData extends Record<string, unknown> {
export interface OfferData extends Record<string, unknown>, Partial<OfferPrice> {
status: OfferStatus;
items: OfferItem[];
// The user that is providing this offer
Expand All @@ -35,8 +42,6 @@ export interface OfferData extends Record<string, unknown> {
offerName?: string;
// Is the offer publicly visible
public?: boolean;
price?: NumberString;
currency?: string;
countryCode?: string;
}

Expand Down
52 changes: 52 additions & 0 deletions src/data/order-item/get-order-item-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {listOrderItems, listOrderProducts} from "./list-order-items";
import {isOrderOfferItem} from "./is";
import {getOffer, DEFAULT_CURRENCY} from "../offer";
import {ok} from "../../is";


export async function getOrderPrice(orderId: string) {
const items = await listOrderItems(orderId);
const offerItems = items.filter(isOrderOfferItem);
const offerIds = [...new Set(
offerItems.map(item => item.offerId)
)];
const offerQuantities = new Map(
offerIds.map(
offerId => {
const matching = offerItems.filter(item => item.offerId === offerId);
const quantity = matching.reduce(
(sum, offer) => {
return sum + (offer.quantity ?? 1);
},
0
);
return [offerId, quantity] as const;
}
)
)
const offers = await Promise.all(
offerIds.map(offerId => getOffer(offerId))
);
const offerMap = new Map(
offers.map(
offer => [offer.offerId, offer] as const
)
)
const currencies = [...new Set(
offers.map(item => item.currency || DEFAULT_CURRENCY)
)];
// TODO if this is required...
ok(currencies.length === 1, "Expected same currency across offers");

const price = offerIds.reduce(
(sum, offerId) => {
return sum + (offerQuantities.get(offerId) * (+offerMap.get(offerId).price))
},
0
)

return {
price,
currency: currencies[0]
}
}
14 changes: 14 additions & 0 deletions src/data/order-item/is.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {OrderItem, OrderOfferItem, OrderProductItem} from "./types";
import {OfferItem, ProductOfferItem} from "../offer";

export function isOrderProductItem(item: OrderItem): item is OrderProductItem {
return !!item.productId;
}

export function isOrderOfferItem(item: OrderItem): item is OrderOfferItem {
return !!item.offerId;
}

export function isProductOfferItem(item: OfferItem): item is ProductOfferItem {
return item.type === "product";
}
13 changes: 1 addition & 12 deletions src/data/order-item/list-order-items.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {OrderItem, OrderOfferItem, OrderProductItem} from "./types";
import { getOrderItemStore } from "./store";
import {getOffer, OfferItem, ProductOfferItem} from "../offer";
import {isOrderOfferItem, isOrderProductItem, isProductOfferItem} from "./is";

export async function listOrderItems(orderId: string): Promise<
OrderItem[]
Expand Down Expand Up @@ -33,16 +34,4 @@ export async function listOrderProducts(orderId: string, offers = true): Promise
return offerProducts
.flatMap<OrderProductItem>(value => value)
.concat(products);

function isOrderProductItem(item: OrderItem): item is OrderProductItem {
return !!item.productId;
}

function isOrderOfferItem(item: OrderItem): item is OrderOfferItem {
return !!item.offerId;
}

function isProductOfferItem(item: OfferItem): item is ProductOfferItem {
return item.type === "product";
}
}
3 changes: 3 additions & 0 deletions src/data/order/list-orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export async function listOrders({ status, from, to }: ListOrdersInput = {}): Pr
function isShipmentLocationMatch(base: ShipmentLocation, match: ShipmentLocation) {
return (
(
!base.userId ||
base.userId === match.userId
) &&(
!base.organisationId ||
base.organisationId === match.organisationId
) &&
Expand Down
2 changes: 2 additions & 0 deletions src/data/order/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface OrderData {
items?: (OrderItemIdentifierData & Partial<OrderItem>)[];
to?: ShipmentTo;
from?: ShipmentFrom; // Is it from a specific known location?
paymentId?: string;
paymentMethodId?: string;
}

export interface Order extends OrderData {
Expand Down
2 changes: 2 additions & 0 deletions src/data/payment-method/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type PaymentMethodStatus = "pending" | "available" | "expired" | "void";
export interface PaymentMethodData extends Record<string, unknown> {
status: PaymentMethodStatus;
type: PaymentMethodType;
userId?: string;
organisationId?: string;
}

export interface PaymentMethod extends PaymentMethodData {
Expand Down
2 changes: 2 additions & 0 deletions src/data/payment/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface PaymentData extends Record<string, unknown> {
status: PaymentStatus;
paymentMethodId: string;
reference?: string;
userId?: string;
organisationId?: string;
}

export interface Payment extends PaymentData {
Expand Down
9 changes: 5 additions & 4 deletions src/data/shipment/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {Identifier} from "../identifier";
export type ShipmentStatus = "pending" | "processing" | "sent" | "delivered";

export interface ShipmentLocation {
organisationId?: string; // Optional fixed organisation
locationId?: string; // Optional fixed location
inventoryId?: string; // Optional fixed inventory set
inventoryItemId?: string; // Optional fixed inventory set
userId?: string;
organisationId?: string;
locationId?: string;
inventoryId?: string;
inventoryItemId?: string;
address?: string[]; // Human-readable address
countryCode?: string;
}
Expand Down
20 changes: 10 additions & 10 deletions src/package.readonly.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// File generated by scripts/pre-build.js

export const commit = "436cb724c806de87b61f94031282ef07934866eb";
export const commitShort = "436cb72";
export const commit = "d18fad03a5ca39ce42fb70d76c0aef5a313d14f1";
export const commitShort = "d18fad0";
export const commitAuthor = "Fabian Cook";
export const commitEmail = "[email protected]";
export const commitMessage = "1.0.0-alpha.25";
export const commitAt = "2023-06-18T10:50:23.000Z";
export const secondsBetweenCommitAndBuild = 590;
export const minutesBetweenCommitAndBuild = 9.83;
export const timeBetweenCommitAndBuild = "9 minutes and 49 seconds";
export const commitMessage = "1.0.0-alpha.26";
export const commitAt = "2023-06-18T11:01:05.000Z";
export const secondsBetweenCommitAndBuild = 44825.99;
export const minutesBetweenCommitAndBuild = 747.1;
export const timeBetweenCommitAndBuild = "747 minutes and 5 seconds";
// Variables to be replaced after tests
export const secondsBetweenCommitAndTestCompletion = "629.48";
export const minutesBetweenCommitAndTestCompletion = "10.49";
export const timeBetweenCommitAndTestCompletion = "10 minutes and 29 seconds";
export const secondsBetweenCommitAndTestCompletion = "44838.79";
export const minutesBetweenCommitAndTestCompletion = "747.31";
export const timeBetweenCommitAndTestCompletion = "747 minutes and 18 seconds";
14 changes: 9 additions & 5 deletions src/tests/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* c8 ignore start */
import why from "why-is-node-still-running";
import {isRedisMemory, seed, startRedisMemory, stopData, stopRedisMemory} from "../data";
import {isRedisMemory, listProducts, seed, startRedisMemory, stopData, stopRedisMemory} from "../data";

try {
if (isRedisMemory()) {
Expand All @@ -9,10 +9,14 @@ try {

await seed();

await import("./client");
console.log("after client");
await import("./remote");
console.log("after remote");
const products = await listProducts();

if (products.length < 3 || !process.env.IS_LOCAL) {
await import("./client");
console.log("after client");
await import("./remote");
console.log("after remote");
}
await import("./scenarios");

// Ensure any data clients are closed
Expand Down
12 changes: 6 additions & 6 deletions src/tests/scenarios/browsing-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const chance = new Chance();
{
let products = await listProducts();

while (products.length < 3) {
await addProduct({
productName: chance.name()
});
products = await listProducts();
}
// while (products.length < 3) {
// await addProduct({
// productName: chance.name()
// });
// products = await listProducts();
// }

ok(products.length >= 3, "Some products should be already seeded")

Expand Down
Loading

0 comments on commit 7c05006

Please sign in to comment.