Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions data-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions data-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
"author": "Demo Project",
"license": "MIT",
"dependencies": {
"express": "^4.18.2",
"async-mutex": "^0.5.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/cors": "^2.8.19",
"@types/express": "^4.17.21",
"@types/node": "^20.8.0",
"@types/uuid": "^9.0.7",
"typescript": "^5.2.2",
"ts-node": "^10.9.1"
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
84 changes: 46 additions & 38 deletions data-service/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Request, Response, NextFunction } from 'express';
import cors from 'cors';
import fs from 'fs/promises';
import { Mutex } from 'async-mutex';
import path from 'path';

interface InventoryItem {
Expand Down Expand Up @@ -48,6 +49,7 @@ class DataService {
private inventoryPath: string;
private cachedOrders: OrderDatabase | null = null;
private cachedInventory: InventoryDatabase | null = null;
private inventoryMutex = new Mutex();

constructor() {
this.ordersPath = path.join(__dirname, '../data/orders.json');
Expand Down Expand Up @@ -251,63 +253,69 @@ class DataService {
}

async reserveStock(productName: string, quantity: number): Promise<boolean> {
const db = await this.loadInventory();
const item = db.inventory.find(
(i) => i.productName.toLowerCase() === productName.toLowerCase()
);
return await this.inventoryMutex.runExclusive(async () => {
const db = await this.loadInventory();
const item = db.inventory.find(
(i) => i.productName.toLowerCase() === productName.toLowerCase()
);

if (!item) {
return false;
}
if (!item) {
return false;
}

const available = item.quantity - item.reservedQuantity;
if (available < quantity) {
return false;
}
const available = item.quantity - item.reservedQuantity;
if (available < quantity) {
return false;
}

item.reservedQuantity += quantity;
item.lastUpdated = new Date().toISOString();
item.reservedQuantity += quantity;
item.lastUpdated = new Date().toISOString();

await this.saveInventory(db);
return true;
await this.saveInventory(db);
return true;
});
}

async fulfillOrder(productName: string, quantity: number): Promise<boolean> {
const db = await this.loadInventory();
const item = db.inventory.find(
(i) => i.productName.toLowerCase() === productName.toLowerCase()
);
return await this.inventoryMutex.runExclusive(async () => {
const db = await this.loadInventory();
const item = db.inventory.find(
(i) => i.productName.toLowerCase() === productName.toLowerCase()
);

if (!item || item.reservedQuantity < quantity) {
return false;
}
if (!item || item.reservedQuantity < quantity) {
return false;
}

item.quantity -= quantity;
item.reservedQuantity -= quantity;
item.lastUpdated = new Date().toISOString();
item.quantity -= quantity;
item.reservedQuantity -= quantity;
item.lastUpdated = new Date().toISOString();

await this.saveInventory(db);
return true;
await this.saveInventory(db);
return true;
});
}

async cancelReservation(
productName: string,
quantity: number
): Promise<boolean> {
const db = await this.loadInventory();
const item = db.inventory.find(
(i) => i.productName.toLowerCase() === productName.toLowerCase()
);
return await this.inventoryMutex.runExclusive(async () => {
const db = await this.loadInventory();
const item = db.inventory.find(
(i) => i.productName.toLowerCase() === productName.toLowerCase()
);

if (!item) {
return false;
}
if (!item) {
return false;
}

item.reservedQuantity = Math.max(0, item.reservedQuantity - quantity);
item.lastUpdated = new Date().toISOString();
item.reservedQuantity = Math.max(0, item.reservedQuantity - quantity);
item.lastUpdated = new Date().toISOString();

await this.saveInventory(db);
return true;
await this.saveInventory(db);
return true;
});
}
}

Expand Down
16 changes: 16 additions & 0 deletions order-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions order-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"license": "MIT",
"dependencies": {
"amqplib": "^0.10.3",
"async-mutex": "^0.5.0",
"axios": "^1.6.0",
"commander": "^11.1.0",
"cors": "^2.8.5",
Expand Down
6 changes: 5 additions & 1 deletion order-service/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Request, Response, NextFunction } from 'express';
import cors from 'cors';
import { v4 as uuidv4 } from 'uuid';
import { Mutex } from 'async-mutex';
import axios, { AxiosError } from 'axios';
import {
Order,
Expand Down Expand Up @@ -58,6 +59,7 @@ app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
class OrderService {
private dataServiceUrl: string;
private products: Product[] = [];
private orderCreationMutex = new Mutex();

constructor(dataServiceUrl: string) {
this.dataServiceUrl = dataServiceUrl;
Expand Down Expand Up @@ -99,7 +101,8 @@ class OrderService {
}

async createOrder(request: OrderCreateRequest): Promise<Order> {
const db = await this.loadDatabase();
return await this.orderCreationMutex.runExclusive(async () => {
const db = await this.loadDatabase();

// Find product in catalog
const product = this.products.find(
Expand Down Expand Up @@ -168,6 +171,7 @@ class OrderService {
);
}
return order;
});
}

async getOrder(orderId: string): Promise<Order | null> {
Expand Down