import { Knex } from 'knex'; import { BaseRepository } from './base-repository'; import { Step, Task } from '@shared/index'; export class StepRepository extends BaseRepository { constructor(db: Knex) { super(db, 'steps'); } async findByTaskId(taskId: number): Promise { return await this.db(this.tableName) .where('task_id', taskId) .orderBy('order') .select('*'); } async incrementPrintCount(id: number): Promise { const step = await this.findById(id); if (!step) return false; return await this.update(id, { print_count: step.print_count + 1, last_printed_at: new Date() }); } async findTaskById(stepId: number): Promise { const step = await this.db('steps') .where({ id: stepId }) .first(); if (!step) { return undefined; } return await this.db('tasks') .where({ id: step.task_id }) .first(); } async findStepNumber(stepId: number): Promise { const step = await this.db('steps') .where({ id: stepId }) .first(); if (!step) { return 0; } const steps = await this.db('steps') .where({ task_id: step.task_id }) .orderBy('order', 'asc') .select('id'); return steps.findIndex(s => s.id === stepId) + 1; } }