Skip to content

Commit

Permalink
Offers, #21
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Jun 18, 2023
1 parent 1183f73 commit d1517df
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 48 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ export interface LocationData extends Record<string, unknown> {
locationName?: string;
address?: string[];
countryCode?: string;
organisationId?: string;
userId?: string;
}

export interface Location extends LocationData {
Expand Down Expand Up @@ -458,6 +460,8 @@ export interface Organisation extends OrganisationData {
approvedByUserId?: string;
}

export type PartialOrganisation = OrganisationData & Partial<Organisation>;

export interface PartnerData extends Record<string, unknown> {
partnerName: string;
countryCode?: string;
Expand Down Expand Up @@ -535,6 +539,7 @@ 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
inventoryProductId?: string; // Optional fixed inventory set
Expand Down
3 changes: 1 addition & 2 deletions src/data/inventory-product/list-inventory-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function listInventoryProducts(options: ListInventoryProductsInput)
> {
// TODO make this not all in memory
// Its okay for now :)
const { inventoryId} = options;
const { inventoryId } = options;
if (!inventoryId) {
const inventory = await listInventory(options);
const values = await Promise.all(
Expand All @@ -27,7 +27,6 @@ export async function listInventoryProducts(options: ListInventoryProductsInput)
return values.flatMap<InventoryProduct>(value => value);
}
const { productId, status } = options;

const store = getInventoryProductStore(inventoryId);
let values = await store.values();
if (productId) {
Expand Down
12 changes: 10 additions & 2 deletions src/data/inventory/list-inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ import {Inventory, InventoryType} from "./types";
import { getInventoryStore } from "./store";

export interface ListInventoryInput {
type?: InventoryType
type?: InventoryType;
organisationId?: string;
userId?: string;
}

export async function listInventory({ type = "inventory" }: ListInventoryInput = {}): Promise<
export async function listInventory({ type = "inventory", organisationId, userId }: ListInventoryInput = {}): Promise<
Inventory[]
> {
const store = getInventoryStore();
let values = await store.values();
if (organisationId) {
values = values.filter(value => value.organisationId === organisationId)
}
if (userId) {
values = values.filter(value => value.userId === userId)
}
if (type) {
values = values.filter(value => value.type === type);
}
Expand Down
2 changes: 2 additions & 0 deletions src/data/inventory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type InventoryType =

export interface InventoryData {
type: InventoryType
userId?: string;
organisationId?: string;
locationId?: string;
products?: (InventoryProductIdentifierData & Partial<InventoryProduct>)[];
}
Expand Down
2 changes: 2 additions & 0 deletions src/data/location/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface LocationData extends Record<string, unknown> {
locationName?: string;
address?: string[];
countryCode?: string;
organisationId?: string;
userId?: string;
}

export interface Location extends LocationData {
Expand Down
8 changes: 8 additions & 0 deletions src/data/offer/list-offers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import { getOfferStore } from "./store";
export interface ListOffersInput {
// Only return public offers, regardless if the user is anonymous
public?: boolean;
productId?: string;
organisationId?: string;
}

export async function listOffers<P extends Offer = Offer>(options: ListOffersInput = {}): Promise<
P[]
> {
const store = getOfferStore<P>();
let offers = await store.values();
if (options.organisationId) {
offers = offers.filter(value => value.organisationId);
}
if (options.public) {
// Force public only
offers = offers.filter(value => value.public);
}
if (options.productId) {
offers = offers.filter(value => value.items.find(item => item.type === "product" ? item.productId === options.productId : false))
}
return offers;
}
55 changes: 54 additions & 1 deletion src/data/order/list-orders.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
import {Order, OrderStatus} from "./types";
import { getOrderStore } from "./store";
import {ShipmentFrom, ShipmentLocation, ShipmentTo} from "../shipment";

export interface ListOrdersInput {
status?: OrderStatus;
from?: ShipmentFrom;
to?: ShipmentTo;
}

export async function listOrders({ status }: ListOrdersInput = {}): Promise<
export async function listOrders({ status, from, to }: ListOrdersInput = {}): Promise<
Order[]
> {
const store = getOrderStore();
let values = await store.values();
if (from) {
values = values.filter(value => {
if (!value.from) return false;
return isShipmentLocationMatch(from, value.from);
});
}
if (to) {
values = values.filter(value => {
if (!value.to) return false;
return isShipmentLocationMatch(to, value.to);
});
}
if (status) {
values = values.filter(value => value.status === status);
}
return values.sort((a, b) => new Date(a.createdAt).getTime() < new Date(b.createdAt).getTime() ? -1 : 1)


}

function isShipmentLocationMatch(base: ShipmentLocation, match: ShipmentLocation) {
return (
(
!base.organisationId ||
base.organisationId === match.organisationId
) &&
(
!base.locationId ||
base.locationId === match.locationId
) &&
(
!base.inventoryId ||
base.inventoryId === match.inventoryId
) &&
(
!base.inventoryProductId ||
base.inventoryProductId === match.inventoryProductId
) &&
(
!base.countryCode ||
base.countryCode === match.countryCode
) &&
(
!base.address ||
(
match.address &&
base.address.length === match.address.length &&
(
!base.address.length ||
base.address.every((value, index) => match.address[index] === value)
)
)
)
)
}
23 changes: 6 additions & 17 deletions src/data/organisation/add-organisation.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { v4 } from "uuid";
import { OrganisationData, Organisation } from "./types";
import { getOrganisationStore } from "./store";
import { setOrganisation } from "./set-organisation";

export interface AddOrganisationInput extends OrganisationData {}

export async function addOrganisation(
data: AddOrganisationInput
): Promise<Organisation> {
const store = getOrganisationStore();
const organisationId = v4();
const createdAt = new Date().toISOString();
const organisation: Organisation = {
export async function addOrganisation(data: OrganisationData): Promise<Organisation> {
return setOrganisation({
...data,
organisationId,
organisationId: v4(),
approved: false,
approvedAt: undefined,
approvedByUserId: undefined,
createdAt,
updatedAt: createdAt,
};
await store.set(organisationId, organisation);
return organisation;
approvedByUserId: undefined
});
}
3 changes: 2 additions & 1 deletion src/data/organisation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./types";
export * from "./get-organisation";
export * from "./list-organisations";
export * from "./add-organisation";
export * as organisationSchema from "./schema";
export * from "./set-organisation";
export * as organisationSchema from "./schema";
23 changes: 23 additions & 0 deletions src/data/organisation/set-organisation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { v4 } from "uuid";
import { Organisation, PartialOrganisation } from "./types";
import { getOrganisationStore } from "./store";

export async function setOrganisation(
data: PartialOrganisation
): Promise<Organisation> {
const store = getOrganisationStore();
const organisationId = data.organisationId || v4();
const updatedAt = new Date().toISOString();
const createdAt = data.createdAt || updatedAt;
const organisation: Organisation = {
approved: false,
approvedAt: undefined,
approvedByUserId: undefined,
...data,
organisationId,
createdAt,
updatedAt,
};
await store.set(organisationId, organisation);
return organisation;
}
2 changes: 2 additions & 0 deletions src/data/organisation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export interface Organisation extends OrganisationData {
updatedAt: string;
approvedByUserId?: string;
}

export type PartialOrganisation = OrganisationData & Partial<Organisation>;
1 change: 1 addition & 0 deletions src/data/shipment/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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
inventoryProductId?: string; // Optional fixed inventory set
Expand Down
16 changes: 8 additions & 8 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 = "c55f5e8843ad844cbe27bb2b6bb5043cb6256444";
export const commitShort = "c55f5e8";
export const commit = "1183f730ea35ee842cbc4d1be6a5fef5c515d156";
export const commitShort = "1183f73";
export const commitAuthor = "Fabian Cook";
export const commitEmail = "[email protected]";
export const commitMessage = "Public offers and products, #21, #1";
export const commitAt = "2023-06-18T04:56:58.000Z";
export const secondsBetweenCommitAndBuild = 107.2;
export const minutesBetweenCommitAndBuild = 1.79;
export const timeBetweenCommitAndBuild = "1 minutes and 47 seconds";
export const secondsBetweenCommitAndBuild = 17513.54;
export const minutesBetweenCommitAndBuild = 291.89;
export const timeBetweenCommitAndBuild = "291 minutes and 53 seconds";
// Variables to be replaced after tests
export const secondsBetweenCommitAndTestCompletion = "";
export const minutesBetweenCommitAndTestCompletion = "";
export const timeBetweenCommitAndTestCompletion = "";
export const secondsBetweenCommitAndTestCompletion = "17547.92";
export const minutesBetweenCommitAndTestCompletion = "292.47";
export const timeBetweenCommitAndTestCompletion = "292 minutes and 27 seconds";
Loading

0 comments on commit d1517df

Please sign in to comment.