Skip to content

Commit

Permalink
[スキーマ変更] readyAt: Date <- orderReady: boolean (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
toririm authored Oct 21, 2024
1 parent 113c496 commit 420dfd9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
34 changes: 30 additions & 4 deletions common/models/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ describe("[unit] order entity", () => {

test("beReady", () => {
const order = OrderEntity.createNew({ orderId: 2024 });
expect(order.orderReady).toBe(false);
expect(order.readyAt).toBe(null);

order.beReady();
expect(order.orderReady).toBe(true);
expect(order.readyAt).not.toBe(null);
expect(order.readyAt).toBeInstanceOf(Date);
});

test("beServed", () => {
Expand All @@ -71,6 +72,31 @@ describe("[unit] order entity", () => {
order.beServed();
expect(order.servedAt).not.toBe(null);
expect(order.servedAt).toBeInstanceOf(Date);
expect(order.readyAt).not.toBe(null);
expect(order.readyAt).toEqual(order.servedAt);
});

test("undoServed & undoReady", () => {
const order = OrderEntity.createNew({ orderId: 2024 });
expect(order.servedAt).toBe(null);

// 提供時には readyAt と servedAt が同じ
order.beServed();
expect(order.servedAt).not.toBe(null);
expect(order.readyAt).not.toBe(null);
expect(order.readyAt).toEqual(order.servedAt);

// undoServed で servedAt も readyAt も null になる
order.undoServed();
expect(order.servedAt).toBe(null);
expect(order.readyAt).toBe(null);

// 別々に設定した場合は undoServed では servedAt だけが null になる
order.beReady();
order.beServed();
order.undoServed();
expect(order.servedAt).toBe(null);
expect(order.readyAt).not.toBe(null);
});

test("billingAmount", () => {
Expand Down Expand Up @@ -102,10 +128,10 @@ describe("[unit] order entity", () => {
id: "1",
orderId: 99999,
createdAt: new Date(),
readyAt: null,
servedAt: null,
items: itemEntities.slice(0, 1),
total: 900,
orderReady: false,
description: null,
billingAmount: 900,
received: 0,
Expand Down Expand Up @@ -159,10 +185,10 @@ describe("[unit] order entity", () => {
id: "1",
orderId: 99999,
createdAt: new Date(),
readyAt: null,
servedAt: null,
items: itemEntities,
total: 900,
orderReady: false,
description: null,
billingAmount: 900,
received: 0,
Expand Down
40 changes: 27 additions & 13 deletions common/models/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export const orderSchema = z.object({
id: z.string().optional(), // Firestore のドキュメント ID
orderId: z.number(),
createdAt: z.date(),
readyAt: z.date().nullable(),
servedAt: z.date().nullable(),
items: z.array(itemSchema.required()),
total: z.number(), // sum of item.price
orderReady: z.boolean(),
description: z.string().nullable(),
billingAmount: z.number(), // total - discount
received: z.number(), // お預かり金額
Expand All @@ -31,10 +31,10 @@ export class OrderEntity implements Order {
private readonly _id: string | undefined,
private _orderId: number,
private _createdAt: Date,
private _readyAt: Date | null,
private _servedAt: Date | null,
private _items: WithId<ItemEntity>[],
private _total: number,
private _orderReady: boolean,
private _description: string | null,
private _billingAmount: number,
private _received: number,
Expand All @@ -50,9 +50,9 @@ export class OrderEntity implements Order {
orderId,
new Date(),
null,
null,
[],
0,
false,
null,
0,
0,
Expand All @@ -72,10 +72,10 @@ export class OrderEntity implements Order {
order.id,
order.orderId,
order.createdAt,
order.readyAt,
order.servedAt,
order.items.map((item) => ItemEntity.fromItem(item)),
order.total,
order.orderReady,
order.description,
order.billingAmount,
order.received,
Expand Down Expand Up @@ -105,6 +105,10 @@ export class OrderEntity implements Order {
return this._createdAt;
}

get readyAt() {
return this._readyAt;
}

get servedAt() {
return this._servedAt;
}
Expand All @@ -124,10 +128,6 @@ export class OrderEntity implements Order {
return this._total;
}

get orderReady() {
return this._orderReady;
}

get description() {
return this._description;
}
Expand Down Expand Up @@ -190,22 +190,36 @@ export class OrderEntity implements Order {
* オーダーを準備完了状態に変更する
*/
beReady() {
// orderReady は false -> true にしか変更できないようにする
this._orderReady = true;
this._readyAt = new Date();
}

/**
* 準備完了状態を取り消す
*/
undoReady() {
this._readyAt = null;
}

/**
* オーダーを提供済み状態に変更する
* もし readyAt が null ならば readyAt を現在時刻に設定する
*/
beServed() {
// servedAt は null -> Date にしか変更できないようにする
this._servedAt = new Date();
const now = new Date();
this._servedAt = now;
if (this._readyAt === null) {
this._readyAt = now;
}
}

/**
* 提供済み状態を取り消す
*/
undoServed() {
// readyAt も同時に更新された場合のみ readyAt を null にする
if (this._readyAt === this._servedAt) {
this._readyAt = null;
}
this._servedAt = null;
}

Expand Down Expand Up @@ -251,10 +265,10 @@ export class OrderEntity implements Order {
id: this.id,
orderId: this.orderId,
createdAt: this.createdAt,
readyAt: this.readyAt,
servedAt: this.servedAt,
items: this.items.map((item) => item.toItem()),
total: this.total,
orderReady: this.orderReady,
description: this.description,
billingAmount: this.billingAmount,
received: this.received,
Expand Down
1 change: 0 additions & 1 deletion pos/app/routes/_header.serve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export default function Serve() {
</div>
))}
</div>
<p>{order.orderReady}</p>
{order?.description && (
<div className="mt-4 flex rounded-md bg-gray-200 p-1">
<div className="flex-none">備考:</div>
Expand Down
1 change: 0 additions & 1 deletion pos/app/routes/orders/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default function Order() {
<p>{order.createdAt.toISOString()}</p>
<p>{`提供時間:${order.servedAt?.toISOString()}`}</p>
<p>{order.total}</p>
<p>{order.orderReady}</p>
</li>
))}
</ul>
Expand Down

0 comments on commit 420dfd9

Please sign in to comment.