task_receipts/server/src/db/repositories/step-repository.ts
2025-06-14 20:49:56 -05:00

57 lines
1.3 KiB
TypeScript

import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { Step, Task } from '@shared/index';
export class StepRepository extends BaseRepository<Step> {
constructor(db: Knex) {
super(db, 'steps');
}
async findByTaskId(taskId: number): Promise<Step[]> {
return await this.db(this.tableName)
.where('task_id', taskId)
.orderBy('order')
.select('*');
}
async incrementPrintCount(id: number): Promise<boolean> {
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<Task | undefined> {
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<number> {
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;
}
}