const newId = require('monotonic-timestamp-base36'); const { toArchiveOrigin } = require('./util'); function getNewAppointmentUrl(archiveUrl, id) { return `${archiveUrl}/appointments/${id || newId()}.json`; } module.exports = class Appointment { constructor (lib) { this.db = lib.db; } async getByYear (year) { await this.db.appointments .where('year').equals(year) .toArray(); } async getByDate (year, month, day) { const date = new Date(year, month, day); const initialDate = date.getTime(); const finalDate = date.setHours(24); await this.db.appointments.query() .where('timestamp').between(initialDate, finalDate) .toArray(); } async insert (archive, { timestamp, label, year }) { const archiveUrl = toArchiveOrigin(archive); const key = newId(); const url = getNewAppointmentUrl(archiveUrl, key); await this.db.appointments.put(url, { key, year, timestamp, label, createdAt: Date.now() }); return url; } };