import {Realm} from "realm"; import {ICType} from "../lib/iccard"; import {AMCardData} from "./AMCardData"; import {AMCardRecentUse} from "./AMCardRecentUse"; import {AMPhysicalCardData} from "./AMPhysicalCardData"; export class AMCard extends Realm.Object implements AMCardData { _id!: Realm.BSON.ObjectId; code: string; name: string; added: Date; lastUsed: Date; recentUses: Realm.Results; physicalCardData?: AMPhysicalCardData | null = null; get isLimitedAimeCard() { // limited cards use the company code 500 return this.physicalCardData?.cardType === ICType.Amusement && this.code.startsWith('500'); } static generate(code: string, name: string) { return { _id: new Realm.BSON.ObjectId(), added: new Date(), code: code, name: name }; } static delete(realm: Realm, card: AMCard) { realm.write(() => { if (card.physicalCardData) { realm.delete(card.physicalCardData); } // remove any usage history tied to the card realm.delete(card.recentUses); realm.delete(card); }); } static schema: Realm.ObjectSchema = { name: "card", primaryKey: "_id", properties: { _id: "objectId", code: "string", name: {type: "string", default: "New Card", indexed: true}, added: {type: "date", default: new Date(), indexed: true}, lastUsed: {type: "date", optional: true, indexed: true}, recentUses: {type: "linkingObjects", objectType: AMCardRecentUse.schema.name, property: "card"}, physicalCardData: {type: "object", objectType: AMPhysicalCardData.schema.name, optional: true} } }; }