MainContext.ts
71 lines
| 2.2 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r107 | import Memory = require("dojo/store/Memory"); | ||
import Observable = require("dojo/store/Observable"); | ||||
cin
|
r110 | import { Appointment, AppointmentRole, Member } from "./Appointment"; | ||
cin
|
r107 | import { Contact } from "./Contact"; | ||
import { Uuid } from "@implab/core-amd/Uuid"; | ||||
cin
|
r110 | import { query } from "@implab/djx/observable"; | ||
import { IDestroyable } from "@implab/core-amd/interfaces"; | ||||
import { delay } from "@implab/core-amd/safe"; | ||||
cin
|
r107 | |||
cin
|
r110 | type AppointmentRecord = Omit<Appointment, "getMembers"> & { id: string }; | ||
cin
|
r107 | |||
type ContactRecord = Contact; | ||||
type MemberRecord = Member & { appointmentId: string; }; | ||||
cin
|
r110 | const item = <T, T2>(map: (x: T) => T2) => <U extends { item: T }>({ item, ...props }: U) => ({ item: map(item), ...props }); | ||
cin
|
r107 | |||
cin
|
r110 | export class MainContext implements IDestroyable { | ||
cin
|
r109 | private readonly _appointments = new Observable(new Memory<AppointmentRecord>()); | ||
cin
|
r107 | |||
cin
|
r109 | private readonly _contacts = new Observable(new Memory<ContactRecord>()); | ||
cin
|
r107 | |||
cin
|
r109 | private readonly _members = new Observable(new Memory<MemberRecord>()); | ||
cin
|
r107 | |||
cin
|
r110 | async createAppointment(title: string, startAt: Date, duration: number, members: Member[]) { | ||
await delay(1000); | ||||
cin
|
r107 | const id = Uuid(); | ||
this._appointments.add({ | ||||
cin
|
r110 | id, | ||
cin
|
r107 | startAt, | ||
duration, | ||||
title | ||||
}); | ||||
members.forEach(member => | ||||
this._members.add({ | ||||
appointmentId: id, | ||||
...member | ||||
cin
|
r110 | }, { id: Uuid() }) as void | ||
cin
|
r107 | ); | ||
} | ||||
cin
|
r110 | queryAppointments({ dateFrom, dateTo }: { dateFrom?: Date; dateTo?: Date; } = {}) { | ||
return query(this._appointments)(({ startAt }) => | ||||
(!dateFrom || dateFrom <= startAt) && | ||||
(!dateTo || startAt <= dateTo) | ||||
).map(item(this._mapAppointment)); | ||||
cin
|
r107 | } | ||
cin
|
r110 | async addMember(appointmentId: string, member: Member) { | ||
await delay(1000); | ||||
this._members.add({ | ||||
appointmentId, | ||||
...member | ||||
}); | ||||
} | ||||
cin
|
r107 | |||
cin
|
r110 | private readonly _mapAppointment = ({ startAt, title, duration, id }: AppointmentRecord) => ({ | ||
id, | ||||
title, | ||||
startAt, | ||||
duration, | ||||
getMembers: (role?: AppointmentRole) => this._members.query(role ? { appointmentId: id, role } : { appointmentId: id }) | ||||
cin
|
r109 | }); | ||
cin
|
r107 | |||
cin
|
r110 | destroy() { | ||
} | ||||
cin
|
r107 | } | ||