import { PaginatedResponse } from '@edr/types'; import { ConflictException, Inject, Injectable, NotFoundException } from '@nestjs/common'; import { CreateShippingLineDto } from '../dto/create-shipping-line.dto'; import { ListRuleEngineQueryDto } from '../dto/list-rule-engine-query.dto'; import { UpdateShippingLineDto } from '../dto/update-shipping-line.dto'; import { ShippingLine } from '../entities/shipping-line.entity'; import { IShippingLinesRepository, SHIPPING_LINES_REPOSITORY, } from '../interfaces/shipping-lines.repository.interface'; @Injectable() export class ShippingLinesService { constructor( @Inject(SHIPPING_LINES_REPOSITORY) private readonly repository: IShippingLinesRepository, ) {} /** List shipping lines — standard paginated envelope with server-side search. */ async findAll(query: ListRuleEngineQueryDto): Promise> { return this.repository.findPaged(query); } /** Get a shipping line by ID. */ async findById(id: string): Promise { const entity = await this.repository.findById(id); if (!entity) throw new NotFoundException(`Shipping line ${id} not found`); return entity; } /** Create a shipping line. */ async create(dto: CreateShippingLineDto): Promise { const existing = await this.repository.findByCode(dto.code); if (existing) throw new ConflictException(`Shipping line with code "${dto.code}" already exists`); return this.repository.create({ code: dto.code, label: dto.label, mappedToCode: dto.mappedToCode, showExtraFeeNotice: dto.showExtraFeeNotice ?? false, isActive: dto.isActive ?? true, }); } /** Update a shipping line. */ async update(id: string, dto: UpdateShippingLineDto): Promise { await this.findById(id); if (dto.code) { const conflict = await this.repository.findByCode(dto.code); if (conflict && conflict.id !== id) { throw new ConflictException(`Shipping line with code "${dto.code}" already exists`); } } const updated = await this.repository.update(id, dto); if (!updated) throw new NotFoundException(`Shipping line ${id} not found`); return updated; } /** Soft-delete a shipping line. */ async remove(id: string): Promise { await this.findById(id); await this.repository.softDelete(id); } }