-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[スキーマ変更] レジの客用画面&オーダーストップ情報共有 (#354)
- Loading branch information
Showing
10 changed files
with
349 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { z } from "zod"; | ||
import { orderSchema } from "./order"; | ||
|
||
export const globalCashierStateSchema = z.object({ | ||
id: z.literal("cashier-state"), | ||
edittingOrder: orderSchema, | ||
}); | ||
|
||
export type GlobalCashierState = z.infer<typeof globalCashierStateSchema>; | ||
|
||
export const orderStatTypes = ["stop", "operational"] as const; | ||
|
||
export const orderStatSchema = z.object({ | ||
createdAt: z.date(), | ||
type: z.enum(orderStatTypes), | ||
}); | ||
|
||
export type OrderStatType = (typeof orderStatTypes)[number]; | ||
export type OrderStat = z.infer<typeof orderStatSchema>; | ||
|
||
export const globalMasterStateSchema = z.object({ | ||
id: z.literal("master-state"), | ||
orderStats: z.array(orderStatSchema), | ||
}); | ||
|
||
export type GlobalMasterState = z.infer<typeof globalMasterStateSchema>; | ||
|
||
export const globalStatSchema = z.union([ | ||
globalCashierStateSchema, | ||
globalMasterStateSchema, | ||
]); | ||
|
||
export type GlobalStat = z.infer<typeof globalStatSchema>; | ||
|
||
export class MasterStateEntity implements GlobalMasterState { | ||
constructor( | ||
public id: "master-state", | ||
private _orderStats: OrderStat[], | ||
) {} | ||
|
||
static fromMasterState(state: GlobalMasterState): MasterStateEntity { | ||
return new MasterStateEntity(state.id, state.orderStats); | ||
} | ||
|
||
static createNew(): MasterStateEntity { | ||
const initOrderStat: OrderStat = { | ||
createdAt: new Date(), | ||
type: "operational", | ||
}; | ||
return new MasterStateEntity("master-state", [initOrderStat]); | ||
} | ||
|
||
get orderStats() { | ||
return this._orderStats; | ||
} | ||
|
||
addOrderStat(stat: OrderStatType) { | ||
this._orderStats.push({ | ||
createdAt: new Date(), | ||
type: stat, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { type Firestore, doc, getDoc, setDoc } from "firebase/firestore"; | ||
import { | ||
cashierStateConverter, | ||
masterStateConverter, | ||
} from "../firebase-utils/converter"; | ||
import { prodDB } from "../firebase-utils/firestore"; | ||
import type { GlobalCashierState, MasterStateEntity } from "../models/global"; | ||
|
||
export type CashierStateRepo = { | ||
get: () => Promise<GlobalCashierState | undefined>; | ||
set: (state: GlobalCashierState) => Promise<void>; | ||
}; | ||
|
||
export type MasterStateRepo = { | ||
get: () => Promise<MasterStateEntity | undefined>; | ||
set: (state: MasterStateEntity) => Promise<void>; | ||
}; | ||
|
||
export const cashierStateRepoFactory = (db: Firestore): CashierStateRepo => { | ||
return { | ||
get: async () => { | ||
const docRef = doc(db, "global", "cashier-state").withConverter( | ||
cashierStateConverter, | ||
); | ||
const docSnap = await getDoc(docRef); | ||
const data = docSnap.data(); | ||
if (data?.id === "cashier-state") { | ||
return data; | ||
} | ||
}, | ||
set: async (state) => { | ||
const docRef = doc(db, "global", "cashier-state").withConverter( | ||
cashierStateConverter, | ||
); | ||
await setDoc(docRef, state); | ||
}, | ||
}; | ||
}; | ||
|
||
export const masterStateRepoFactory = (db: Firestore): MasterStateRepo => { | ||
return { | ||
get: async () => { | ||
const docRef = doc(db, "global", "master-state").withConverter( | ||
masterStateConverter, | ||
); | ||
const docSnap = await getDoc(docRef); | ||
const data = docSnap.data(); | ||
if (data?.id === "master-state") { | ||
return data; | ||
} | ||
}, | ||
set: async (state) => { | ||
const docRef = doc(db, "global", "master-state").withConverter( | ||
masterStateConverter, | ||
); | ||
await setDoc(docRef, state); | ||
}, | ||
}; | ||
}; | ||
|
||
export const cashierRepository = cashierStateRepoFactory(prodDB); | ||
export const masterRepository = masterStateRepoFactory(prodDB); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { OrderEntity } from "common/models/order"; | ||
import { useEffect } from "react"; | ||
|
||
export const useSyncCahiserOrder = ( | ||
order: OrderEntity, | ||
syncOrder: (order: OrderEntity) => void, | ||
) => { | ||
useEffect(() => { | ||
syncOrder(order); | ||
}, [order, syncOrder]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.