Add field management module with database schema and validation
- Introduced `FieldModule` to manage cycles and plans, including read and write controllers. - Created database migrations for `company_settings`, `cycles`, `cycle_weekdays`, `cycle_destinations`, `plans`, `plan_destinations`, `plan_invoices`, and `plan_packing_slips` tables, including constraints and unique indexes. - Developed service and repository layers for handling cycle and plan data operations. - Added unit tests for the cycles and plans services, repositories, and controllers to ensure functionality and correctness. - Updated application module to include the new `FieldModule` for better organization.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { DateTime } from '../../../common/value-objects/date-time/date-time';
|
||||
import { Status } from '../../../common/value-objects/status/status';
|
||||
import { DRIZZLE, type DrizzleDB } from '../../../database/database.module';
|
||||
import {
|
||||
companySettings,
|
||||
type CompanySettingsRow,
|
||||
} from '../../../database/company-settings-table';
|
||||
import type {
|
||||
CompanySetting,
|
||||
UpsertCompanySettingInput,
|
||||
} from './company-setting';
|
||||
|
||||
@Injectable()
|
||||
export class CompanySettingsRepository {
|
||||
constructor(@Inject(DRIZZLE) private readonly db: DrizzleDB) {}
|
||||
|
||||
async find(): Promise<CompanySetting | null> {
|
||||
const rows: CompanySettingsRow[] = await this.db
|
||||
.select()
|
||||
.from(companySettings)
|
||||
.limit(1);
|
||||
const row = rows[0];
|
||||
return row ? this.toDomain(row) : null;
|
||||
}
|
||||
|
||||
async upsert(input: UpsertCompanySettingInput): Promise<CompanySetting> {
|
||||
const now = DateTime.fromUnixMs(Date.now());
|
||||
const existing = await this.find();
|
||||
if (!existing) {
|
||||
const inserted = await this.db
|
||||
.insert(companySettings)
|
||||
.values({
|
||||
cycleStartDate: input.cycleStartDate.value,
|
||||
status: Status.create(Status.DEFAULT).value,
|
||||
createdAt: now.value,
|
||||
updatedAt: now.value,
|
||||
createdBy: input.userId,
|
||||
updatedBy: input.userId,
|
||||
})
|
||||
.returning();
|
||||
return this.toDomain(inserted[0]);
|
||||
}
|
||||
const updated = await this.db
|
||||
.update(companySettings)
|
||||
.set({
|
||||
cycleStartDate: input.cycleStartDate.value,
|
||||
updatedAt: now.value,
|
||||
updatedBy: input.userId,
|
||||
})
|
||||
.where(eq(companySettings.id, existing.id))
|
||||
.returning();
|
||||
return this.toDomain(updated[0]);
|
||||
}
|
||||
|
||||
private toDomain(row: CompanySettingsRow): CompanySetting {
|
||||
return {
|
||||
id: row.id,
|
||||
cycleStartDate: DateTime.fromUnixMs(row.cycleStartDate),
|
||||
status: Status.create(row.status),
|
||||
createdAt: DateTime.fromUnixMs(row.createdAt),
|
||||
updatedAt: DateTime.fromUnixMs(row.updatedAt),
|
||||
createdBy: row.createdBy,
|
||||
updatedBy: row.updatedBy,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user