import { BaseRepository } from "@edr/api-common";
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";

import {
  ContractTemplate,
  ContractTemplateCode,
} from "./entities/contract-template.entity";

@Injectable()
export class ContractTemplatesRepository extends BaseRepository<ContractTemplate> {
  constructor(
    @InjectRepository(ContractTemplate)
    repository: Repository<ContractTemplate>,
  ) {
    super(repository);
  }

  findByCode(code: ContractTemplateCode): Promise<ContractTemplate | null> {
    return this.repository.findOne({ where: { code } });
  }

  override findAll(): Promise<ContractTemplate[]> {
    return this.repository.find({ order: { code: "ASC" } });
  }

  async saveTemplate(template: ContractTemplate): Promise<ContractTemplate> {
    return this.repository.save(template);
  }
}
