85 lines
2.5 KiB
TypeScript
85 lines
2.5 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import { StorageService } from "../storage/storage.service";
|
|
import { CreateTaxReturnDto } from "./dto/create-return.dto";
|
|
import { UpdateTaxReturnDto } from "./dto/update-return.dto";
|
|
|
|
@Injectable()
|
|
export class TaxService {
|
|
constructor(private readonly storage: StorageService) {}
|
|
|
|
async listReturns(userId?: string) {
|
|
const snapshot = await this.storage.load();
|
|
return userId
|
|
? snapshot.taxReturns.filter((ret) => ret.userId === userId)
|
|
: [];
|
|
}
|
|
|
|
async createReturn(payload: CreateTaxReturnDto) {
|
|
const snapshot = await this.storage.load();
|
|
const now = this.storage.now();
|
|
const next = {
|
|
id: this.storage.createId(),
|
|
userId: payload.userId,
|
|
taxYear: payload.taxYear,
|
|
filingType: payload.filingType,
|
|
jurisdictions: payload.jurisdictions,
|
|
status: "draft" as const,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
summary: {}
|
|
};
|
|
snapshot.taxReturns.push(next);
|
|
await this.storage.save(snapshot);
|
|
return next;
|
|
}
|
|
|
|
async updateReturn(id: string, payload: UpdateTaxReturnDto) {
|
|
const snapshot = await this.storage.load();
|
|
const index = snapshot.taxReturns.findIndex((ret) => ret.id === id);
|
|
if (index === -1) {
|
|
return null;
|
|
}
|
|
const existing = snapshot.taxReturns[index];
|
|
const next = {
|
|
...existing,
|
|
status: payload.status ?? existing.status,
|
|
summary: payload.summary ?? existing.summary,
|
|
updatedAt: this.storage.now()
|
|
};
|
|
snapshot.taxReturns[index] = next;
|
|
await this.storage.save(snapshot);
|
|
return next;
|
|
}
|
|
|
|
async addDocument(returnId: string, docType: string, metadata: Record<string, unknown>) {
|
|
const snapshot = await this.storage.load();
|
|
const doc = {
|
|
id: this.storage.createId(),
|
|
taxReturnId: returnId,
|
|
docType,
|
|
metadata,
|
|
createdAt: this.storage.now()
|
|
};
|
|
snapshot.taxDocuments.push(doc);
|
|
await this.storage.save(snapshot);
|
|
return doc;
|
|
}
|
|
|
|
async exportReturn(id: string) {
|
|
const snapshot = await this.storage.load();
|
|
const taxReturn = snapshot.taxReturns.find((ret) => ret.id === id);
|
|
if (!taxReturn) {
|
|
return null;
|
|
}
|
|
const docs = snapshot.taxDocuments.filter((doc) => doc.taxReturnId === id);
|
|
const payload = {
|
|
return: taxReturn,
|
|
documents: docs
|
|
};
|
|
taxReturn.status = "exported";
|
|
taxReturn.updatedAt = this.storage.now();
|
|
await this.storage.save(snapshot);
|
|
return payload;
|
|
}
|
|
}
|