Skip to content

Instantly share code, notes, and snippets.

@okaprinarjaya
Created December 10, 2023 12:23
Show Gist options
  • Save okaprinarjaya/286e623f06400f2c5eced50ca8ae4c8f to your computer and use it in GitHub Desktop.
Save okaprinarjaya/286e623f06400f2c5eced50ca8ae4c8f to your computer and use it in GitHub Desktop.
Stages-Watch Driven Development - uuhhuuyy!
type Stage = {
name: string
description: string
status: string
};
class StagesWatcher {
private _stages: Stage[];
constructor() {
this._stages;
}
public async stage<T>(stageName: string, callback: T | unknown): Promise<unknown> {
const aStage: Stage | undefined = this._stages.find(stage => stage.name === stageName);
if (aStage && typeof callback === "function") {
try {
aStage.status = "inprogress";
const results = await callback();
aStage.status = "success";
return results;
} catch (error) {
aStage.status = "error";
return null;
}
}
throw new Error("No stage");
}
}
export class OrderService {
private _dataSource: DataSource;
private _stagesWatcher: StagesWatcher;
constructor(dataSource: DataSource) {
this._dataSource = dataSource;
this._stagesWatcher = new StagesWatcher([
{
name: "Check stock",
description: "Check stock details description - describe here as details as you can bruh!",
status: "pending"
},
{
name: "Resolve product items details",
description: "Resolve product items details (name, price) details description - describe here as details as you can bruh!",
status: "pending"
},
{
name: "Stage 03",
description: "Stage 03 details description - describe here as details as you can bruh!",
status: "pending"
},
{
name: "Stage 04",
description: "Stage 03 details description - describe here as details as you can bruh!",
status: "pending"
},
{
name: "Record order to DB",
description: "Record order to DB details description - describe here as details as you can bruh!",
status: "pending"
},
]);
}
private async stage(stageName: string, callback: any) {
return this._stagesWatcher.stage(stageName, callback);
}
public async createOrder(payload: PayloadCreateOrder) {
const stock = await this.stage(
"Check stock",
() => {
return await checkStockBruh(payload);
}
);
if (stock) {
const products = await this.stage(
"Resolve product items details",
async () => {
return await productItemDetailsResolver(payload.items)
}
);
const nextProcess03 = await this.stage(
"",
async () => {
return await somethingNeeded03();
}
);
const nextProcess04 = await this.stage(
"",
async () => {
return await somethingNeeded04();
}
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment