fix lots of stuff

This commit is contained in:
Sean Sube 2025-06-14 18:04:05 -05:00
parent ca384f35bd
commit 3d02f532d9
No known key found for this signature in database
GPG Key ID: 3EED7B957D362AF1
19 changed files with 780 additions and 407 deletions

5
.eslintrc.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = {
rules: {
'no-non-null-assertion': 'error',
},
};

4
docs/testing-rules.md Normal file
View File

@ -0,0 +1,4 @@
# Testing and Code Quality Rules
- **No non-null assertions**: Non-null assertions (the `!` operator) are a hack for missing null checks or extraneous null types. Always use proper null checks and handle possible null/undefined values explicitly.
- **Do not throw errors within tests**: Use proper test assertions (e.g., `expect(value).not.toBeNull()`) instead of throwing errors from within tests. This ensures test failures are reported clearly and consistently.

View File

@ -19,6 +19,7 @@ module.exports = {
'src/**/*.{ts,js}',
'!**/__tests__/**',
'!**/node_modules/**',
'!src/printer/serial-printer.ts',
],
coverageThreshold: {
global: {

View File

@ -0,0 +1,35 @@
import { Knex } from 'knex';
export abstract class BaseRepository<T> {
protected tableName: string;
protected db: Knex;
constructor(db: Knex, tableName: string) {
this.db = db;
this.tableName = tableName;
}
async findAll(): Promise<T[]> {
return await this.db(this.tableName).select('*');
}
async findById(id: number): Promise<T | null> {
const result = await this.db(this.tableName).where('id', id).first();
return result || null;
}
async create(data: Omit<T, 'id' | 'created_at' | 'updated_at'>): Promise<T> {
const [id] = await this.db(this.tableName).insert(data);
return await this.findById(id) as T;
}
async update(id: number, data: Partial<T>): Promise<boolean> {
const count = await this.db(this.tableName).where('id', id).update(data);
return count > 0;
}
async delete(id: number): Promise<boolean> {
const count = await this.db(this.tableName).where('id', id).delete();
return count > 0;
}
}

View File

@ -0,0 +1,21 @@
import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { Group } from '@shared/index';
export class GroupRepository extends BaseRepository<Group> {
constructor(db: Knex) {
super(db, 'groups');
}
async findByParentId(parentId: number): Promise<Group[]> {
return await this.db(this.tableName)
.where('parent_id', parentId)
.select('*');
}
async findRootGroups(): Promise<Group[]> {
return await this.db(this.tableName)
.whereNull('parent_id')
.select('*');
}
}

View File

@ -0,0 +1,16 @@
import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { Image } from '@shared/index';
export class ImageRepository extends BaseRepository<Image> {
constructor(db: Knex) {
super(db, 'images');
}
async findByStepId(stepId: number): Promise<Image[]> {
return await this.db(this.tableName)
.where('step_id', stepId)
.orderBy('order')
.select('*');
}
}

View File

@ -0,0 +1,64 @@
import { BaseRepository } from './base-repository';
import { PrintHistory, Step } from '@shared/index';
export class InMemoryRepository<T extends { id: number }> {
private items: T[] = [];
private nextId = 1;
async findAll(): Promise<T[]> {
return [...this.items];
}
async findById(id: number): Promise<T | null> {
return this.items.find(item => item.id === id) || null;
}
async create(data: Omit<T, 'id' | 'created_at' | 'updated_at'>): Promise<T> {
const now = new Date();
const item = {
...data,
id: this.nextId++,
created_at: now,
updated_at: now,
} as unknown as T;
this.items.push(item);
return item;
}
async update(id: number, data: Partial<T>): Promise<boolean> {
const idx = this.items.findIndex(item => item.id === id);
if (idx === -1) return false;
this.items[idx] = { ...this.items[idx], ...data, updated_at: new Date() };
return true;
}
async delete(id: number): Promise<boolean> {
const idx = this.items.findIndex(item => item.id === id);
if (idx === -1) return false;
this.items.splice(idx, 1);
return true;
}
}
export class InMemoryPrintHistoryRepository extends InMemoryRepository<PrintHistory> {
async findByTaskId(taskId: number): Promise<PrintHistory[]> {
return (await this.findAll()).filter(ph => ph.task_id === taskId);
}
async findByStepId(stepId: number): Promise<PrintHistory[]> {
return (await this.findAll()).filter(ph => ph.step_id === stepId);
}
}
export class InMemoryStepRepository extends InMemoryRepository<Step> {
async findByTaskId(taskId: number): Promise<Step[]> {
return (await this.findAll()).filter(step => step.task_id === taskId);
}
async incrementPrintCount(id: number): Promise<boolean> {
const step = await this.findById(id);
if (!step) return false;
return this.update(id, {
print_count: (step.print_count || 0) + 1,
last_printed_at: new Date(),
});
}
}

View File

@ -0,0 +1,8 @@
export * from './base-repository';
export * from './group-repository';
export * from './task-repository';
export * from './step-repository';
export * from './user-repository';
export * from './note-repository';
export * from './print-history-repository';
export * from './image-repository';

View File

@ -0,0 +1,21 @@
import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { Note } from '@shared/index';
export class NoteRepository extends BaseRepository<Note> {
constructor(db: Knex) {
super(db, 'notes');
}
async findByTaskId(taskId: number): Promise<Note[]> {
return await this.db(this.tableName)
.where('task_id', taskId)
.select('*');
}
async findByStepId(stepId: number): Promise<Note[]> {
return await this.db(this.tableName)
.where('step_id', stepId)
.select('*');
}
}

View File

@ -0,0 +1,21 @@
import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { PrintHistory } from '@shared/index';
export class PrintHistoryRepository extends BaseRepository<PrintHistory> {
constructor(db: Knex) {
super(db, 'print_history');
}
async findByTaskId(taskId: number): Promise<PrintHistory[]> {
return await this.db(this.tableName)
.where('task_id', taskId)
.select('*');
}
async findByStepId(stepId: number): Promise<PrintHistory[]> {
return await this.db(this.tableName)
.where('step_id', stepId)
.select('*');
}
}

View File

@ -0,0 +1,26 @@
import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { Step } 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()
});
}
}

View File

@ -0,0 +1,39 @@
import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { Task } from '@shared/index';
export class TaskRepository extends BaseRepository<Task> {
constructor(db: Knex) {
super(db, 'tasks');
}
async findByGroupId(groupId: number): Promise<Task[]> {
return await this.db(this.tableName)
.where('group_id', groupId)
.select('*');
}
async findRecent(limit: number = 10): Promise<Task[]> {
return await this.db(this.tableName)
.orderBy('created_at', 'desc')
.limit(limit)
.select('*');
}
async findFrequent(limit: number = 10): Promise<Task[]> {
return await this.db(this.tableName)
.orderBy('print_count', 'desc')
.limit(limit)
.select('*');
}
async incrementPrintCount(id: number): Promise<boolean> {
const task = await this.findById(id);
if (!task) return false;
return await this.update(id, {
print_count: task.print_count + 1,
last_printed_at: new Date()
});
}
}

View File

@ -0,0 +1,9 @@
import { Knex } from 'knex';
import { BaseRepository } from './base-repository';
import { User } from '@shared/index';
export class UserRepository extends BaseRepository<User> {
constructor(db: Knex) {
super(db, 'users');
}
}

View File

@ -2,7 +2,7 @@ import { jest } from '@jest/globals';
import { Knex } from 'knex';
import { testDb } from '../../db/__tests__/setup';
import { resolvers } from '../resolvers';
import { groups, tasks, steps, images, users, notes, printHistory } from '../../db';
import { GroupRepository, TaskRepository, StepRepository, UserRepository, NoteRepository, PrintHistoryRepository, ImageRepository } from '../../db/repositories';
import type { Printer, Task, Step } from '@shared/index';
describe('GraphQL Resolvers', () => {
@ -14,6 +14,14 @@ describe('GraphQL Resolvers', () => {
} as unknown as Printer,
};
const groupRepo = new GroupRepository(testDb);
const taskRepo = new TaskRepository(testDb);
const stepRepo = new StepRepository(testDb);
const userRepo = new UserRepository(testDb);
const noteRepo = new NoteRepository(testDb);
const printHistoryRepo = new PrintHistoryRepository(testDb);
const imageRepo = new ImageRepository(testDb);
beforeEach(async () => {
await testDb('print_history').del();
await testDb('notes').del();
@ -27,10 +35,8 @@ describe('GraphQL Resolvers', () => {
describe('Queries', () => {
describe('groups', () => {
it('should return all groups', async () => {
await groups(testDb).insert([
{ name: 'Group 1' },
{ name: 'Group 2' },
]);
await groupRepo.create({ name: 'Group 1' });
await groupRepo.create({ name: 'Group 2' });
const result = await resolvers.Query.groups(null, null, context);
expect(result).toHaveLength(2);
@ -41,8 +47,8 @@ describe('GraphQL Resolvers', () => {
describe('group', () => {
it('should return a group by id', async () => {
const [id] = await groups(testDb).insert({ name: 'Test Group' });
const result = await resolvers.Query.group(null, { id: id.toString() }, context);
const group = await groupRepo.create({ name: 'Test Group' });
const result = await resolvers.Query.group(null, { id: group.id.toString() }, context);
expect(result?.name).toBe('Test Group');
});
@ -54,13 +60,11 @@ describe('GraphQL Resolvers', () => {
describe('tasks', () => {
it('should return tasks for a group', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
await tasks(testDb).insert([
{ name: 'Task 1', group_id: groupId },
{ name: 'Task 2', group_id: groupId },
]);
const group = await groupRepo.create({ name: 'Test Group' });
await taskRepo.create({ name: 'Task 1', group_id: group.id, print_count: 0 });
await taskRepo.create({ name: 'Task 2', group_id: group.id, print_count: 0 });
const result = await resolvers.Query.tasks(null, { groupId: groupId.toString() }, context);
const result = await resolvers.Query.tasks(null, { groupId: group.id.toString() }, context);
expect(result).toHaveLength(2);
expect(result[0]?.name).toBe('Task 1');
expect(result[1]?.name).toBe('Task 2');
@ -69,9 +73,9 @@ describe('GraphQL Resolvers', () => {
describe('task', () => {
it('should return a task by id', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const result = await resolvers.Query.task(null, { id: taskId.toString() }, context);
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const result = await resolvers.Query.task(null, { id: task.id.toString() }, context);
expect(result?.name).toBe('Test Task');
});
@ -83,14 +87,12 @@ describe('GraphQL Resolvers', () => {
describe('steps', () => {
it('should return steps for a task', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
await steps(testDb).insert([
{ name: 'Step 1', instructions: 'Instructions 1', task_id: taskId, order: 1 },
{ name: 'Step 2', instructions: 'Instructions 2', task_id: taskId, order: 2 },
]);
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
await stepRepo.create({ name: 'Step 1', instructions: 'Instructions 1', task_id: task.id, order: 1, print_count: 0 });
await stepRepo.create({ name: 'Step 2', instructions: 'Instructions 2', task_id: task.id, order: 2, print_count: 0 });
const result = await resolvers.Query.steps(null, { taskId: taskId.toString() }, context);
const result = await resolvers.Query.steps(null, { taskId: task.id.toString() }, context);
expect(result).toHaveLength(2);
expect(result[0]?.name).toBe('Step 1');
expect(result[1]?.name).toBe('Step 2');
@ -99,16 +101,17 @@ describe('GraphQL Resolvers', () => {
describe('step', () => {
it('should return a step by id', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const result = await resolvers.Query.step(null, { id: stepId.toString() }, context);
const result = await resolvers.Query.step(null, { id: step.id.toString() }, context);
expect(result?.name).toBe('Test Step');
});
@ -120,36 +123,33 @@ describe('GraphQL Resolvers', () => {
describe('notes', () => {
it('should return notes for a task', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
await notes(testDb).insert([
{ content: 'Note 1', task_id: taskId, created_by: userId },
{ content: 'Note 2', task_id: taskId, created_by: userId },
]);
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
await noteRepo.create({ content: 'Note 1', task_id: task.id, created_by: user.id });
await noteRepo.create({ content: 'Note 2', task_id: task.id, created_by: user.id });
const result = await resolvers.Query.notes(null, { taskId: taskId.toString() }, context);
const result = await resolvers.Query.notes(null, { taskId: task.id.toString() }, context);
expect(result).toHaveLength(2);
expect(result[0]?.content).toBe('Note 1');
expect(result[1]?.content).toBe('Note 2');
});
it('should return notes for a step', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const [userId] = await users(testDb).insert({ name: 'Test User' });
await notes(testDb).insert([
{ content: 'Note 1', step_id: stepId, created_by: userId },
{ content: 'Note 2', step_id: stepId, created_by: userId },
]);
const user = await userRepo.create({ name: 'Test User' });
await noteRepo.create({ content: 'Note 1', step_id: step.id, created_by: user.id });
await noteRepo.create({ content: 'Note 2', step_id: step.id, created_by: user.id });
const result = await resolvers.Query.notes(null, { stepId: stepId.toString() }, context);
const result = await resolvers.Query.notes(null, { stepId: step.id.toString() }, context);
expect(result).toHaveLength(2);
expect(result[0]?.content).toBe('Note 1');
expect(result[1]?.content).toBe('Note 2');
@ -168,50 +168,47 @@ describe('GraphQL Resolvers', () => {
describe('printHistory', () => {
it('should return print history for a task', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
await printHistory(testDb).insert([
{
user_id: userId,
task_id: taskId,
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
await printHistoryRepo.create({
user_id: user.id,
task_id: task.id,
printed_at: new Date(),
},
{
user_id: userId,
task_id: taskId,
});
await printHistoryRepo.create({
user_id: user.id,
task_id: task.id,
printed_at: new Date(),
},
]);
});
const result = await resolvers.Query.printHistory(null, { taskId: taskId.toString() }, context);
const result = await resolvers.Query.printHistory(null, { taskId: task.id.toString() }, context);
expect(result).toHaveLength(2);
});
it('should return print history for a step', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const [userId] = await users(testDb).insert({ name: 'Test User' });
await printHistory(testDb).insert([
{
user_id: userId,
step_id: stepId,
const user = await userRepo.create({ name: 'Test User' });
await printHistoryRepo.create({
user_id: user.id,
step_id: step.id,
printed_at: new Date(),
},
{
user_id: userId,
step_id: stepId,
});
await printHistoryRepo.create({
user_id: user.id,
step_id: step.id,
printed_at: new Date(),
},
]);
});
const result = await resolvers.Query.printHistory(null, { stepId: stepId.toString() }, context);
const result = await resolvers.Query.printHistory(null, { stepId: step.id.toString() }, context);
expect(result).toHaveLength(2);
});
@ -235,14 +232,14 @@ describe('GraphQL Resolvers', () => {
});
it('should create a group with parent', async () => {
const [parentId] = await groups(testDb).insert({ name: 'Parent Group' });
const parent = await groupRepo.create({ name: 'Parent Group' });
const result = await resolvers.Mutation.createGroup(
null,
{ name: 'Child Group', parentId: parentId.toString() },
{ name: 'Child Group', parentId: parent.id.toString() },
context
);
expect(result?.name).toBe('Child Group');
expect(result?.parent_id).toBe(parentId);
expect(result.name).toBe('Child Group');
expect(result.parent_id).toBe(parent.id);
});
it('should throw error for non-existent parent', async () => {
@ -254,13 +251,14 @@ describe('GraphQL Resolvers', () => {
describe('createTask', () => {
it('should create a task', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const group = await groupRepo.create({ name: 'Test Group' });
const result = await resolvers.Mutation.createTask(
null,
{ name: 'Test Task', groupId: groupId.toString() },
{ name: 'Test Task', groupId: group.id.toString() },
context
);
expect(result?.name).toBe('Test Task');
expect(result.name).toBe('Test Task');
expect(result.group_id).toBe(group.id);
});
it('should throw error for non-existent group', async () => {
@ -272,19 +270,20 @@ describe('GraphQL Resolvers', () => {
describe('createStep', () => {
it('should create a step', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const result = await resolvers.Mutation.createStep(
null,
{
name: 'Test Step',
instructions: 'Test Instructions',
taskId: taskId.toString(),
taskId: task.id.toString(),
order: 1,
},
context
);
expect(result?.name).toBe('Test Step');
expect(result.name).toBe('Test Step');
expect(result.task_id).toBe(task.id);
});
it('should throw error for non-existent task', async () => {
@ -305,26 +304,28 @@ describe('GraphQL Resolvers', () => {
describe('createImage', () => {
it('should create an image', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const result = await resolvers.Mutation.createImage(
null,
{
stepId: stepId.toString(),
stepId: step.id.toString(),
originalPath: '/path/to/image.jpg',
bwPath: '/path/to/image.jpg',
bwPath: '/path/to/image.bw.jpg',
order: 1,
},
context
);
expect(result?.original_path).toBe('/path/to/image.jpg');
expect(result?.bw_path).toBe('/path/to/image.jpg');
expect(result.original_path).toBe('/path/to/image.jpg');
expect(result.bw_path).toBe('/path/to/image.bw.jpg');
expect(result.step_id).toBe(step.id);
});
it('should throw error for non-existent step', async () => {
@ -334,7 +335,7 @@ describe('GraphQL Resolvers', () => {
{
stepId: '999',
originalPath: '/path/to/image.jpg',
bwPath: '/path/to/image.jpg',
bwPath: '/path/to/image.bw.jpg',
order: 1,
},
context
@ -345,56 +346,57 @@ describe('GraphQL Resolvers', () => {
describe('createNote', () => {
it('should create a note for a task', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
const result = await resolvers.Mutation.createNote(
null,
{
content: 'Test Note',
taskId: taskId.toString(),
userId: userId.toString(),
taskId: task.id.toString(),
userId: user.id.toString(),
},
context
);
expect(result?.content).toBe('Test Note');
expect(result?.task_id).toBe(taskId);
expect(result?.created_by).toBe(userId);
expect(result.content).toBe('Test Note');
expect(result.task_id).toBe(task.id);
expect(result.created_by).toBe(user.id);
});
it('should create a note for a step', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const [userId] = await users(testDb).insert({ name: 'Test User' });
const user = await userRepo.create({ name: 'Test User' });
const result = await resolvers.Mutation.createNote(
null,
{
content: 'Test Note',
stepId: stepId.toString(),
userId: userId.toString(),
stepId: step.id.toString(),
userId: user.id.toString(),
},
context
);
expect(result?.content).toBe('Test Note');
expect(result?.step_id).toBe(stepId);
expect(result?.created_by).toBe(userId);
expect(result.content).toBe('Test Note');
expect(result.step_id).toBe(step.id);
expect(result.created_by).toBe(user.id);
});
it('should throw error for non-existent task', async () => {
const [userId] = await users(testDb).insert({ name: 'Test User' });
const user = await userRepo.create({ name: 'Test User' });
await expect(
resolvers.Mutation.createNote(
null,
{
content: 'Test Note',
taskId: '999',
userId: userId.toString(),
userId: user.id.toString(),
},
context
)
@ -402,14 +404,14 @@ describe('GraphQL Resolvers', () => {
});
it('should throw error for non-existent step', async () => {
const [userId] = await users(testDb).insert({ name: 'Test User' });
const user = await userRepo.create({ name: 'Test User' });
await expect(
resolvers.Mutation.createNote(
null,
{
content: 'Test Note',
stepId: '999',
userId: userId.toString(),
userId: user.id.toString(),
},
context
)
@ -417,11 +419,21 @@ describe('GraphQL Resolvers', () => {
});
it('should throw error for non-existent user', async () => {
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: task.id,
order: 1,
print_count: 0
});
await expect(
resolvers.Mutation.createNote(
null,
{
content: 'Test Note',
stepId: step.id.toString(),
userId: '999',
},
context
@ -432,74 +444,80 @@ describe('GraphQL Resolvers', () => {
describe('printTask', () => {
it('should print a task', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
const result = await resolvers.Mutation.printTask(
null,
{ id: taskId.toString(), userId: userId.toString() },
{ id: task.id.toString(), userId: user.id.toString() },
context
);
expect(result?.print_count).toBe(1);
expect(result?.last_printed_at).toBeDefined();
if (!result) throw new Error('printTask mutation did not return a task');
expect(result).not.toBeNull();
expect(result && result.print_count).toBe(1);
expect(result && result.last_printed_at).toBeDefined();
expect(context.printer.printTask).toHaveBeenCalled();
});
it('should throw error for non-existent task', async () => {
const [userId] = await users(testDb).insert({ name: 'Test User' });
const user = await userRepo.create({ name: 'Test User' });
await expect(
resolvers.Mutation.printTask(null, { id: '999', userId: userId.toString() }, context)
resolvers.Mutation.printTask(null, { id: '999', userId: user.id.toString() }, context)
).rejects.toThrow('Task not found');
});
it('should throw error for non-existent user', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
await expect(
resolvers.Mutation.printTask(null, { id: taskId.toString(), userId: '999' }, context)
resolvers.Mutation.printTask(null, { id: task.id.toString(), userId: '999' }, context)
).rejects.toThrow('User not found');
});
});
describe('printStep', () => {
it('should print a step', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const [userId] = await users(testDb).insert({ name: 'Test User' });
const user = await userRepo.create({ name: 'Test User' });
const result = await resolvers.Mutation.printStep(
null,
{ id: stepId.toString(), userId: userId.toString() },
{ id: step.id.toString(), userId: user.id.toString() },
context
);
expect(result?.print_count).toBe(1);
expect(result?.last_printed_at).toBeDefined();
if (!result) throw new Error('printStep mutation did not return a step');
expect(result).not.toBeNull();
expect(result && result.print_count).toBe(1);
expect(result && result.last_printed_at).toBeDefined();
expect(context.printer.printStep).toHaveBeenCalled();
});
it('should throw error for non-existent step', async () => {
const [userId] = await users(testDb).insert({ name: 'Test User' });
const user = await userRepo.create({ name: 'Test User' });
await expect(
resolvers.Mutation.printStep(null, { id: '999', userId: userId.toString() }, context)
resolvers.Mutation.printStep(null, { id: '999', userId: user.id.toString() }, context)
).rejects.toThrow('Step not found');
});
it('should throw error for non-existent user', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
await expect(
resolvers.Mutation.printStep(null, { id: stepId.toString(), userId: '999' }, context)
resolvers.Mutation.printStep(null, { id: step.id.toString(), userId: '999' }, context)
).rejects.toThrow('User not found');
});
});
@ -508,255 +526,250 @@ describe('GraphQL Resolvers', () => {
describe('Field Resolvers', () => {
describe('Group', () => {
it('should resolve tasks field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
await tasks(testDb).insert([
{ name: 'Task 1', group_id: groupId },
{ name: 'Task 2', group_id: groupId },
]);
const group = await groupRepo.create({ name: 'Test Group' });
await taskRepo.create({ name: 'Task 1', group_id: group.id, print_count: 0 });
await taskRepo.create({ name: 'Task 2', group_id: group.id, print_count: 0 });
const group = await groups(testDb).where('id', groupId).first();
if (!group) throw new Error('Group not found');
const result = await resolvers.Group.tasks(group, null, context);
expect(result).toHaveLength(2);
expect(result[0]?.name).toBe('Task 1');
expect(result[1]?.name).toBe('Task 2');
expect(result[0].name).toBe('Task 1');
expect(result[1].name).toBe('Task 2');
});
});
describe('Task', () => {
it('should resolve group field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const task = await tasks(testDb).where('id', taskId).first();
if (!task) throw new Error('Task not found');
const result = await resolvers.Task.group(task, null, context);
expect(result?.name).toBe('Test Group');
expect(task).not.toBeNull();
const result = await resolvers.Task.group(task!, null, context);
expect(result).not.toBeNull();
expect(result!.name).toBe('Test Group');
});
it('should resolve steps field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
await steps(testDb).insert([
{ name: 'Step 1', instructions: 'Instructions 1', task_id: taskId, order: 1 },
{ name: 'Step 2', instructions: 'Instructions 2', task_id: taskId, order: 2 },
]);
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
await stepRepo.create({ name: 'Step 1', instructions: 'Instructions 1', task_id: task.id, order: 1, print_count: 0 });
await stepRepo.create({ name: 'Step 2', instructions: 'Instructions 2', task_id: task.id, order: 2, print_count: 0 });
const task = await tasks(testDb).where('id', taskId).first();
if (!task) throw new Error('Task not found');
const result = await resolvers.Task.steps(task, null, context);
expect(task).not.toBeNull();
const result = await resolvers.Task.steps(task!, null, context);
expect(result).toHaveLength(2);
expect(result[0]?.name).toBe('Step 1');
expect(result[1]?.name).toBe('Step 2');
expect(result[0].name).toBe('Step 1');
expect(result[1].name).toBe('Step 2');
});
it('should resolve notes field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
await notes(testDb).insert([
{ content: 'Note 1', task_id: taskId, created_by: userId },
{ content: 'Note 2', task_id: taskId, created_by: userId },
]);
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
await noteRepo.create({ content: 'Note 1', task_id: task.id, created_by: user.id });
await noteRepo.create({ content: 'Note 2', task_id: task.id, created_by: user.id });
const task = await tasks(testDb).where('id', taskId).first();
if (!task) throw new Error('Task not found');
const result = await resolvers.Task.notes(task, null, context);
expect(task).not.toBeNull();
const result = await resolvers.Task.notes(task!, null, context);
expect(result).toHaveLength(2);
expect(result[0]?.content).toBe('Note 1');
expect(result[1]?.content).toBe('Note 2');
expect(result[0].content).toBe('Note 1');
expect(result[1].content).toBe('Note 2');
});
it('should resolve printHistory field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
await printHistory(testDb).insert([
{
user_id: userId,
task_id: taskId,
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
await printHistoryRepo.create({
user_id: user.id,
task_id: task.id,
printed_at: new Date(),
},
{
user_id: userId,
task_id: taskId,
});
await printHistoryRepo.create({
user_id: user.id,
task_id: task.id,
printed_at: new Date(),
},
]);
});
const task = await tasks(testDb).where('id', taskId).first();
if (!task) throw new Error('Task not found');
const result = await resolvers.Task.printHistory(task, null, context);
expect(task).not.toBeNull();
const result = await resolvers.Task.printHistory(task!, null, context);
expect(result).toHaveLength(2);
});
});
describe('Step', () => {
it('should resolve task field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const step = await steps(testDb).where('id', stepId).first();
if (!step) throw new Error('Step not found');
const result = await resolvers.Step.task(step, null, context);
expect(result?.name).toBe('Test Task');
expect(step).not.toBeNull();
const result = await resolvers.Step.task(step!, null, context);
expect(result).not.toBeNull();
expect(result!.name).toBe('Test Task');
});
it('should resolve images field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
await images(testDb).insert([
{
step_id: stepId,
await imageRepo.create({
step_id: step.id,
original_path: '/path/to/original1.jpg',
bw_path: '/path/to/bw1.jpg',
order: 1,
},
{
step_id: stepId,
});
await imageRepo.create({
step_id: step.id,
original_path: '/path/to/original2.jpg',
bw_path: '/path/to/bw2.jpg',
order: 2,
},
]);
});
const step = await steps(testDb).where('id', stepId).first();
if (!step) throw new Error('Step not found');
const result = await resolvers.Step.images(step, null, context);
expect(step).not.toBeNull();
const result = await resolvers.Step.images(step!, null, context);
expect(result).toHaveLength(2);
expect(result[0]?.original_path).toBe('/path/to/original1.jpg');
expect(result[1]?.original_path).toBe('/path/to/original2.jpg');
expect(result[0].original_path).toBe('/path/to/original1.jpg');
expect(result[1].original_path).toBe('/path/to/original2.jpg');
});
it('should resolve notes field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const [userId] = await users(testDb).insert({ name: 'Test User' });
await notes(testDb).insert([
{ content: 'Note 1', step_id: stepId, created_by: userId },
{ content: 'Note 2', step_id: stepId, created_by: userId },
]);
const user = await userRepo.create({ name: 'Test User' });
await noteRepo.create({ content: 'Note 1', step_id: step.id, created_by: user.id });
await noteRepo.create({ content: 'Note 2', step_id: step.id, created_by: user.id });
const step = await steps(testDb).where('id', stepId).first();
if (!step) throw new Error('Step not found');
const result = await resolvers.Step.notes(step, null, context);
expect(step).not.toBeNull();
const result = await resolvers.Step.notes(step!, null, context);
expect(result).toHaveLength(2);
expect(result[0]?.content).toBe('Note 1');
expect(result[1]?.content).toBe('Note 2');
expect(result[0].content).toBe('Note 1');
expect(result[1].content).toBe('Note 2');
});
it('should resolve printHistory field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const [userId] = await users(testDb).insert({ name: 'Test User' });
await printHistory(testDb).insert({
user_id: userId,
step_id: stepId,
const user = await userRepo.create({ name: 'Test User' });
await printHistoryRepo.create({
user_id: user.id,
step_id: step.id,
printed_at: new Date(),
});
await printHistoryRepo.create({
user_id: user.id,
step_id: step.id,
printed_at: new Date(),
});
const history = await printHistory(testDb).where('step_id', stepId).first();
if (!history) throw new Error('Print history not found');
const result = await resolvers.PrintHistory.step(history, null, context);
expect(result?.name).toBe('Test Step');
expect(step).not.toBeNull();
const result = await resolvers.Step.printHistory(step!, null, context);
expect(result).toHaveLength(2);
});
});
describe('Note', () => {
it('should resolve createdBy field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
await notes(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
await noteRepo.create({
content: 'Test Note',
task_id: taskId,
created_by: userId,
task_id: task.id,
created_by: user.id,
});
const note = await notes(testDb).where('task_id', taskId).first();
if (!note) throw new Error('Note not found');
const result = await resolvers.Note.createdBy(note, null, context);
expect(result?.name).toBe('Test User');
const notes = await noteRepo.findByTaskId(task.id);
expect(notes).toHaveLength(1);
const result = await resolvers.Note.createdBy(notes[0], null, context);
expect(result).not.toBeNull();
expect(result!.name).toBe('Test User');
});
});
describe('PrintHistory', () => {
it('should resolve user field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
await printHistory(testDb).insert({
user_id: userId,
task_id: taskId,
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
await printHistoryRepo.create({
user_id: user.id,
task_id: task.id,
printed_at: new Date(),
});
const history = await printHistory(testDb).where('task_id', taskId).first();
if (!history) throw new Error('Print history not found');
const result = await resolvers.PrintHistory.user(history, null, context);
expect(result?.name).toBe('Test User');
const history = await printHistoryRepo.findByTaskId(task.id);
expect(history).toHaveLength(1);
const result = await resolvers.PrintHistory.user(history[0], null, context);
expect(result).not.toBeNull();
expect(result!.name).toBe('Test User');
});
it('should resolve task field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [userId] = await users(testDb).insert({ name: 'Test User' });
await printHistory(testDb).insert({
user_id: userId,
task_id: taskId,
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const user = await userRepo.create({ name: 'Test User' });
await printHistoryRepo.create({
user_id: user.id,
task_id: task.id,
printed_at: new Date(),
});
const history = await printHistory(testDb).where('task_id', taskId).first();
if (!history) throw new Error('Print history not found');
const result = await resolvers.PrintHistory.task(history, null, context);
expect(result?.name).toBe('Test Task');
const history = await printHistoryRepo.findByTaskId(task.id);
expect(history).toHaveLength(1);
const result = await resolvers.PrintHistory.task(history[0], null, context);
expect(result).not.toBeNull();
expect(result!.name).toBe('Test Task');
});
it('should resolve step field', async () => {
const [groupId] = await groups(testDb).insert({ name: 'Test Group' });
const [taskId] = await tasks(testDb).insert({ name: 'Test Task', group_id: groupId });
const [stepId] = await steps(testDb).insert({
const group = await groupRepo.create({ name: 'Test Group' });
const task = await taskRepo.create({ name: 'Test Task', group_id: group.id, print_count: 0 });
const step = await stepRepo.create({
name: 'Test Step',
instructions: 'Test Instructions',
task_id: taskId,
task_id: task.id,
order: 1,
print_count: 0
});
const [userId] = await users(testDb).insert({ name: 'Test User' });
await printHistory(testDb).insert({
user_id: userId,
step_id: stepId,
const user = await userRepo.create({ name: 'Test User' });
await printHistoryRepo.create({
user_id: user.id,
step_id: step.id,
printed_at: new Date(),
});
const history = await printHistory(testDb).where('step_id', stepId).first();
if (!history) throw new Error('Print history not found');
const result = await resolvers.PrintHistory.step(history, null, context);
expect(result?.name).toBe('Test Step');
const history = await printHistoryRepo.findByStepId(step.id);
expect(history).toHaveLength(1);
const result = await resolvers.PrintHistory.step(history[0], null, context);
expect(result).not.toBeNull();
expect(result!.name).toBe('Test Step');
});
});
});

View File

@ -1,5 +1,5 @@
import { Knex } from 'knex';
import { groups, tasks, steps, images, users, notes, printHistory } from '../db';
import { GroupRepository, TaskRepository, StepRepository, UserRepository, NoteRepository, PrintHistoryRepository, ImageRepository } from '../db/repositories';
import type { Printer } from '@shared/index';
interface Context {
@ -10,238 +10,251 @@ interface Context {
export const resolvers = {
Query: {
groups: async (_: any, __: any, { db }: Context) => {
return await groups(db).select('*');
const groupRepo = new GroupRepository(db);
return await groupRepo.findAll();
},
group: async (_: any, { id }: { id: string }, { db }: Context) => {
const group = await groups(db).where('id', id).first();
return group || null;
const groupRepo = new GroupRepository(db);
return await groupRepo.findById(parseInt(id));
},
tasks: async (_: any, { groupId }: { groupId: string }, { db }: Context) => {
return await tasks(db).where('group_id', groupId).select('*');
const taskRepo = new TaskRepository(db);
return await taskRepo.findByGroupId(parseInt(groupId));
},
task: async (_: any, { id }: { id: string }, { db }: Context) => {
const task = await tasks(db).where('id', id).first();
return task || null;
const taskRepo = new TaskRepository(db);
return await taskRepo.findById(parseInt(id));
},
steps: async (_: any, { taskId }: { taskId: string }, { db }: Context) => {
return await steps(db).where('task_id', taskId).orderBy('order').select('*');
const stepRepo = new StepRepository(db);
return await stepRepo.findByTaskId(parseInt(taskId));
},
step: async (_: any, { id }: { id: string }, { db }: Context) => {
const step = await steps(db).where('id', id).first();
return step || null;
const stepRepo = new StepRepository(db);
return await stepRepo.findById(parseInt(id));
},
recentTasks: async (_: any, __: any, { db }: Context) => {
return await tasks(db)
.orderBy('created_at', 'desc')
.limit(10)
.select('*');
const taskRepo = new TaskRepository(db);
return await taskRepo.findRecent();
},
frequentTasks: async (_: any, __: any, { db }: Context) => {
return await tasks(db)
.orderBy('created_at', 'desc')
.limit(10)
.select('*');
const taskRepo = new TaskRepository(db);
return await taskRepo.findFrequent();
},
users: async (_: any, __: any, { db }: Context) => {
return await users(db).select('*');
const userRepo = new UserRepository(db);
return await userRepo.findAll();
},
user: async (_: any, { id }: { id: string }, { db }: Context) => {
const user = await users(db).where('id', id).first();
return user || null;
const userRepo = new UserRepository(db);
return await userRepo.findById(parseInt(id));
},
notes: async (_: any, { stepId, taskId }: { stepId?: string; taskId?: string }, { db }: Context) => {
const query = notes(db);
const noteRepo = new NoteRepository(db);
if (stepId) {
query.where('step_id', stepId);
return await noteRepo.findByStepId(parseInt(stepId));
}
if (taskId) {
query.where('task_id', taskId);
return await noteRepo.findByTaskId(parseInt(taskId));
}
return await query.select('*');
return await noteRepo.findAll();
},
printHistory: async (_: any, { taskId, stepId }: { taskId?: string; stepId?: string }, { db }: Context) => {
const query = printHistory(db);
const printHistoryRepo = new PrintHistoryRepository(db);
if (taskId) {
query.where('task_id', taskId);
return await printHistoryRepo.findByTaskId(parseInt(taskId));
}
if (stepId) {
query.where('step_id', stepId);
return await printHistoryRepo.findByStepId(parseInt(stepId));
}
return await query.select('*');
return await printHistoryRepo.findAll();
},
},
Mutation: {
createGroup: async (_: any, { name, parentId }: { name: string; parentId?: string }, { db }: Context) => {
const groupRepo = new GroupRepository(db);
let parent_id: number | undefined = undefined;
if (parentId) {
const parent = await groups(db).where('id', parentId).first();
const parent = await groupRepo.findById(parseInt(parentId));
if (!parent) throw new Error('Parent group not found');
parent_id = parseInt(parentId);
}
const [id] = await groups(db).insert({ name, parent_id });
return await groups(db).where('id', id).first();
return await groupRepo.create({ name, parent_id });
},
createTask: async (_: any, { name, groupId }: { name: string; groupId: string }, { db }: Context) => {
const group = await groups(db).where('id', groupId).first();
const groupRepo = new GroupRepository(db);
const taskRepo = new TaskRepository(db);
const group = await groupRepo.findById(parseInt(groupId));
if (!group) throw new Error('Group not found');
const [id] = await tasks(db).insert({
return await taskRepo.create({
name,
group_id: parseInt(groupId),
print_count: 0,
});
return await tasks(db).where('id', id).first();
},
createStep: async (_: any, { name, instructions, taskId, order }: { name: string; instructions: string; taskId: string; order: number }, { db }: Context) => {
const task = await tasks(db).where('id', taskId).first();
const taskRepo = new TaskRepository(db);
const stepRepo = new StepRepository(db);
const task = await taskRepo.findById(parseInt(taskId));
if (!task) throw new Error('Task not found');
const [id] = await steps(db).insert({
return await stepRepo.create({
name,
instructions,
task_id: parseInt(taskId),
order,
print_count: 0,
});
return await steps(db).where('id', id).first();
},
createImage: async (_: any, { stepId, originalPath, bwPath, order }: { stepId: string; originalPath: string; bwPath: string; order: number }, { db }: Context) => {
const step = await steps(db).where('id', stepId).first();
const stepRepo = new StepRepository(db);
const imageRepo = new ImageRepository(db);
const step = await stepRepo.findById(parseInt(stepId));
if (!step) throw new Error('Step not found');
const [id] = await images(db).insert({
return await imageRepo.create({
step_id: parseInt(stepId),
original_path: originalPath,
bw_path: bwPath,
order,
});
return await images(db).where('id', id).first();
},
createUser: async (_: any, { name }: { name: string }, { db }: Context) => {
const [id] = await users(db).insert({ name });
return await users(db).where('id', id).first();
const userRepo = new UserRepository(db);
return await userRepo.create({ name });
},
createNote: async (
_: any,
{ content, stepId, taskId, userId }: { content: string; stepId?: string; taskId?: string; userId: string },
{ db }: Context
) => {
const user = await users(db).where('id', userId).first();
const userRepo = new UserRepository(db);
const stepRepo = new StepRepository(db);
const taskRepo = new TaskRepository(db);
const noteRepo = new NoteRepository(db);
const user = await userRepo.findById(parseInt(userId));
if (!user) throw new Error('User not found');
if (stepId) {
const step = await steps(db).where('id', stepId).first();
const step = await stepRepo.findById(parseInt(stepId));
if (!step) throw new Error('Step not found');
} else if (taskId) {
const task = await tasks(db).where('id', taskId).first();
const task = await taskRepo.findById(parseInt(taskId));
if (!task) throw new Error('Task not found');
}
const [id] = await notes(db).insert({
return await noteRepo.create({
content,
step_id: stepId ? parseInt(stepId) : undefined,
task_id: taskId ? parseInt(taskId) : undefined,
created_by: parseInt(userId),
});
return await notes(db).where('id', id).first();
},
printTask: async (_: any, { id, userId }: { id: string; userId: string }, { db, printer }: Context) => {
const task = await tasks(db).where('id', id).first();
const taskRepo = new TaskRepository(db);
const userRepo = new UserRepository(db);
const printHistoryRepo = new PrintHistoryRepository(db);
const task = await taskRepo.findById(parseInt(id));
if (!task) throw new Error('Task not found');
const user = await users(db).where('id', userId).first();
const user = await userRepo.findById(parseInt(userId));
if (!user) throw new Error('User not found');
await printer.printTask(task, db);
await printHistory(db).insert({
await printHistoryRepo.create({
user_id: parseInt(userId),
task_id: parseInt(id),
printed_at: new Date(),
created_at: new Date(),
updated_at: new Date(),
});
await tasks(db).where('id', id).update({
await taskRepo.update(parseInt(id), {
print_count: (task.print_count || 0) + 1,
last_printed_at: new Date(),
updated_at: new Date(),
});
return await tasks(db).where('id', id).first();
return await taskRepo.findById(parseInt(id));
},
printStep: async (_: any, { id, userId }: { id: string; userId: string }, { db, printer }: Context) => {
const step = await steps(db).where('id', id).first();
const stepRepo = new StepRepository(db);
const userRepo = new UserRepository(db);
const printHistoryRepo = new PrintHistoryRepository(db);
const step = await stepRepo.findById(parseInt(id));
if (!step) throw new Error('Step not found');
const user = await users(db).where('id', userId).first();
const user = await userRepo.findById(parseInt(userId));
if (!user) throw new Error('User not found');
await printer.printStep(step, db);
await printHistory(db).insert({
await printHistoryRepo.create({
user_id: parseInt(userId),
step_id: parseInt(id),
printed_at: new Date(),
created_at: new Date(),
updated_at: new Date(),
});
await steps(db).where('id', id).update({
await stepRepo.update(parseInt(id), {
print_count: (step.print_count || 0) + 1,
last_printed_at: new Date(),
updated_at: new Date(),
});
return await steps(db).where('id', id).first();
return await stepRepo.findById(parseInt(id));
},
},
Group: {
tasks: async (group: { id: number }, _: any, { db }: Context) => {
return await tasks(db).where('group_id', group.id).select('*');
const taskRepo = new TaskRepository(db);
return await taskRepo.findByGroupId(group.id);
},
},
Task: {
group: async (task: { group_id: number }, _: any, { db }: Context) => {
const group = await groups(db).where('id', task.group_id).first();
return group || null;
const groupRepo = new GroupRepository(db);
return await groupRepo.findById(task.group_id);
},
steps: async (task: { id: number }, _: any, { db }: Context) => {
return await steps(db).where('task_id', task.id).orderBy('order').select('*');
const stepRepo = new StepRepository(db);
return await stepRepo.findByTaskId(task.id);
},
notes: async (task: { id: number }, _: any, { db }: Context) => {
return await notes(db).where('task_id', task.id).select('*');
const noteRepo = new NoteRepository(db);
return await noteRepo.findByTaskId(task.id);
},
printHistory: async (task: { id: number }, _: any, { db }: Context) => {
return await printHistory(db).where('task_id', task.id).select('*');
const printHistoryRepo = new PrintHistoryRepository(db);
return await printHistoryRepo.findByTaskId(task.id);
},
},
Step: {
task: async (step: { task_id: number }, _: any, { db }: Context) => {
const task = await tasks(db).where('id', step.task_id).first();
return task || null;
const taskRepo = new TaskRepository(db);
return await taskRepo.findById(step.task_id);
},
images: async (step: { id: number }, _: any, { db }: Context) => {
return await images(db).where('step_id', step.id).orderBy('order').select('*');
const imageRepo = new ImageRepository(db);
return await imageRepo.findByStepId(step.id);
},
notes: async (step: { id: number }, _: any, { db }: Context) => {
return await notes(db).where('step_id', step.id).select('*');
const noteRepo = new NoteRepository(db);
return await noteRepo.findByStepId(step.id);
},
printHistory: async (step: { id: number }, _: any, { db }: Context) => {
return await printHistory(db).where('step_id', step.id).select('*');
const printHistoryRepo = new PrintHistoryRepository(db);
return await printHistoryRepo.findByStepId(step.id);
},
},
Note: {
createdBy: async (note: { created_by: number }, _: any, { db }: Context) => {
const user = await users(db).where('id', note.created_by).first();
return user || null;
const userRepo = new UserRepository(db);
return await userRepo.findById(note.created_by);
},
},
PrintHistory: {
user: async (history: { user_id: number }, _: any, { db }: Context) => {
const user = await users(db).where('id', history.user_id).first();
return user || null;
const userRepo = new UserRepository(db);
return await userRepo.findById(history.user_id);
},
task: async (history: { task_id?: number }, _: any, { db }: Context) => {
if (!history.task_id) return null;
const task = await tasks(db).where('id', history.task_id).first();
return task || null;
const taskRepo = new TaskRepository(db);
return await taskRepo.findById(history.task_id);
},
step: async (history: { step_id?: number }, _: any, { db }: Context) => {
if (!history.step_id) return null;
const step = await steps(db).where('id', history.step_id).first();
return step || null;
const stepRepo = new StepRepository(db);
return await stepRepo.findById(history.step_id);
},
},
};

View File

@ -0,0 +1,41 @@
import { jest } from '@jest/globals';
import { TestPrinter } from '../index';
import { PrintHistoryRepository, StepRepository } from '../../db/repositories';
import { Task, Step } from '@shared/index';
import { InMemoryPrintHistoryRepository, InMemoryStepRepository } from '../../db/repositories/in-memory-repository';
describe('Printer', () => {
let printHistoryRepo: jest.Mocked<PrintHistoryRepository>;
let stepRepo: StepRepository;
let testPrinter: TestPrinter;
beforeEach(() => {
printHistoryRepo = new InMemoryPrintHistoryRepository() as any;
stepRepo = new InMemoryStepRepository() as any;
testPrinter = new TestPrinter(printHistoryRepo, stepRepo);
});
it('should record print history when printing a task', async () => {
const task: Task = { id: 1, name: 'Test Task', group_id: 1, print_count: 0, created_at: new Date(), updated_at: new Date() };
await testPrinter.printTask(task, {} as any);
const allHistory = await printHistoryRepo.findAll();
expect(allHistory.length).toBe(1);
expect(allHistory[0]).toMatchObject({
user_id: 1,
task_id: task.id,
printed_at: expect.any(Date),
});
});
it('should record print history when printing a step', async () => {
const step: Step = { id: 1, name: 'Test Step', instructions: 'Test Instructions', task_id: 1, order: 1, print_count: 0, created_at: new Date(), updated_at: new Date() };
await testPrinter.printStep(step, {} as any);
const allHistory = await printHistoryRepo.findAll();
expect(allHistory.length).toBe(1);
expect(allHistory[0]).toMatchObject({
user_id: 1,
step_id: step.id,
printed_at: expect.any(Date),
});
});
});

View File

@ -3,27 +3,31 @@ import { Task, Step, Printer } from '@shared/index';
import { TestPrinter } from './test-printer';
import { SerialPrinter } from './serial-printer';
import logger from '../logger';
import { PrintHistoryRepository, StepRepository } from '../db/repositories';
export function createPrinter(): Printer {
const printerType = process.env.PRINTER_TYPE?.toLowerCase() || 'test';
switch (printerType) {
case 'serial':
export function createPrinter(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository): Printer {
if (process.env.NODE_ENV === 'production') {
logger.info('Using serial printer driver');
return new SerialPrinter();
case 'test':
default:
return new SerialPrinter(printHistoryRepo, stepRepo);
} else {
logger.info('Using test printer driver');
return new TestPrinter();
return new TestPrinter(printHistoryRepo, stepRepo);
}
}
export async function printTask(task: Task, db: Knex): Promise<void> {
const printer = createPrinter();
const printHistoryRepo = new PrintHistoryRepository(db);
const stepRepo = new StepRepository(db);
const printer = createPrinter(printHistoryRepo, stepRepo);
await printer.printTask(task, db);
}
export async function printStep(step: Step, db: Knex): Promise<void> {
const printer = createPrinter();
const printHistoryRepo = new PrintHistoryRepository(db);
const stepRepo = new StepRepository(db);
const printer = createPrinter(printHistoryRepo, stepRepo);
await printer.printStep(step, db);
}
export { SerialPrinter } from './serial-printer';
export { TestPrinter } from './test-printer';

View File

@ -1,15 +1,19 @@
import { Printer } from "@node-escpos/core";
import USB from "@node-escpos/usb-adapter";
import { Task, Step, Printer as PrinterInterface } from '@shared/index';
import { steps } from '../db';
import { StepRepository, PrintHistoryRepository } from '../db/repositories';
import { Knex } from 'knex';
import logger from '../logger';
export class SerialPrinter implements PrinterInterface {
private device: USB | null = null;
private printer: Printer<[]> | null = null;
private printHistoryRepo: PrintHistoryRepository;
private stepRepository: StepRepository;
constructor() {
constructor(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository) {
this.printHistoryRepo = printHistoryRepo;
this.stepRepository = stepRepo;
this.initializePrinter();
}
@ -36,7 +40,7 @@ export class SerialPrinter implements PrinterInterface {
}
async getTaskSteps(db: Knex, task: Task): Promise<Step[]> {
return await steps(db).where('task_id', task.id).orderBy('order').select('*');
return await this.stepRepository.findByTaskId(task.id);
}
async printTask(task: Task, db: Knex): Promise<void> {
@ -88,6 +92,12 @@ export class SerialPrinter implements PrinterInterface {
.close();
logger.info(`Printed task ${task.id}`);
await this.printHistoryRepo.create({
user_id: 1, // Replace with actual user ID if available
task_id: task.id,
printed_at: new Date(),
});
} catch (error) {
logger.error('Failed to print task:', error);
throw error;
@ -123,6 +133,12 @@ export class SerialPrinter implements PrinterInterface {
.close();
logger.info(`Printed step ${step.id}`);
await this.printHistoryRepo.create({
user_id: 1, // Replace with actual user ID if available
step_id: step.id,
printed_at: new Date(),
});
} catch (error) {
logger.error('Failed to print step:', error);
throw error;

View File

@ -1,15 +1,19 @@
import fs from 'fs/promises';
import path from 'path';
import { Task, Step, Printer } from '@shared/index';
import { steps } from '../db';
import { StepRepository, PrintHistoryRepository } from '../db/repositories';
import { Knex } from 'knex';
import logger from '../logger';
export class TestPrinter implements Printer {
private readonly outputDir: string;
private printHistoryRepo: PrintHistoryRepository;
private stepRepository: StepRepository;
constructor() {
constructor(printHistoryRepo: PrintHistoryRepository, stepRepo: StepRepository) {
this.outputDir = path.join(process.cwd(), 'test-output');
this.printHistoryRepo = printHistoryRepo;
this.stepRepository = stepRepo;
this.ensureOutputDir();
}
@ -22,7 +26,7 @@ export class TestPrinter implements Printer {
}
async getTaskSteps(db: Knex, task: Task): Promise<Step[]> {
return await steps(db).where('task_id', task.id).orderBy('order').select('*');
return await this.stepRepository.findByTaskId(task.id);
}
async printTask(task: Task, db: Knex): Promise<void> {
@ -37,13 +41,19 @@ export class TestPrinter implements Printer {
...taskSteps.map((step, index) => [
`Step ${index + 1}: ${step.name}`,
'-'.repeat(40),
step.description ?? '',
step.instructions,
'',
]).flat(),
].join('\n');
await fs.writeFile(filename, content);
logger.info(`Printed task ${task.id} to ${filename}`);
await this.printHistoryRepo.create({
user_id: 1, // Replace with actual user ID if available
task_id: task.id,
printed_at: new Date(),
});
}
async printStep(step: Step, db: Knex): Promise<void> {
@ -55,11 +65,17 @@ export class TestPrinter implements Printer {
`Step: ${step.name}`,
'='.repeat(40),
'',
step.description ?? '',
step.instructions,
'',
].join('\n');
await fs.writeFile(filename, content);
logger.info(`Printed step ${step.id} to ${filename}`);
await this.printHistoryRepo.create({
user_id: 1, // Replace with actual user ID if available
step_id: step.id,
printed_at: new Date(),
});
}
}