112 lines
3.2 KiB
TypeScript
112 lines
3.2 KiB
TypeScript
import { BadRequestException, Injectable } from '@nestjs/common';
|
|||
|
|
import type {
|
||
|
|
TahesabInvoiceSettingsDto,
|
||
|
|
UpdateTahesabInvoiceSettingsRequest,
|
||
|
|
} from '@mento/shared';
|
||
|
|
import { PrismaService } from '../../prisma/prisma.service';
|
||
|
|
|
||
|
|
const MAX_LOGO_CHARS = 700_000; // ~500KB data URL
|
||
|
|
|
||
|
|
function toDto(row: {
|
||
|
|
id: string;
|
||
|
|
organizationId: string;
|
||
|
|
logoDataUrl: string | null;
|
||
|
|
companyName: string;
|
||
|
|
companySubtitle: string;
|
||
|
|
address: string;
|
||
|
|
phone: string;
|
||
|
|
headerText: string;
|
||
|
|
footerText: string;
|
||
|
|
updatedAt: Date;
|
||
|
|
}): TahesabInvoiceSettingsDto {
|
||
|
|
return {
|
||
|
|
id: row.id,
|
||
|
|
organizationId: row.organizationId,
|
||
|
|
logoDataUrl: row.logoDataUrl,
|
||
|
|
companyName: row.companyName,
|
||
|
|
companySubtitle: row.companySubtitle,
|
||
|
|
address: row.address,
|
||
|
|
phone: row.phone,
|
||
|
|
headerText: row.headerText,
|
||
|
|
footerText: row.footerText,
|
||
|
|
updatedAt: row.updatedAt.toISOString(),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class TahesabInvoiceSettingsService {
|
||
|
|
constructor(private readonly prisma: PrismaService) {}
|
||
|
|
|
||
|
|
async getOrCreate(organizationId: string): Promise<TahesabInvoiceSettingsDto> {
|
||
|
|
const existing = await this.prisma.tahesabInvoiceSettings.findUnique({
|
||
|
|
where: { organizationId },
|
||
|
|
});
|
||
|
|
if (existing) return toDto(existing);
|
||
|
|
|
||
|
|
const org = await this.prisma.organization.findUnique({
|
||
|
|
where: { id: organizationId },
|
||
|
|
select: { name: true },
|
||
|
|
});
|
||
|
|
const created = await this.prisma.tahesabInvoiceSettings.create({
|
||
|
|
data: {
|
||
|
|
organizationId,
|
||
|
|
companyName: org?.name ?? '',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
return toDto(created);
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(
|
||
|
|
organizationId: string,
|
||
|
|
dto: UpdateTahesabInvoiceSettingsRequest,
|
||
|
|
): Promise<TahesabInvoiceSettingsDto> {
|
||
|
|
if (dto.logoDataUrl != null && dto.logoDataUrl.length > MAX_LOGO_CHARS) {
|
||
|
|
throw new BadRequestException(
|
||
|
|
'حجم لوگو زیاد است؛ تصویر کوچکتری انتخاب کنید.',
|
||
|
|
);
|
||
|
|
}
|
||
|
|
if (
|
||
|
|
dto.logoDataUrl != null &&
|
||
|
|
dto.logoDataUrl !== '' &&
|
||
|
|
!/^data:image\/(png|jpe?g|webp|gif);base64,/i.test(dto.logoDataUrl)
|
||
|
|
) {
|
||
|
|
throw new BadRequestException('لوگو باید تصویر با فرمت data URL باشد.');
|
||
|
|
}
|
||
|
|
|
||
|
|
const data = {
|
||
|
|
...(dto.logoDataUrl !== undefined
|
||
|
|
? { logoDataUrl: dto.logoDataUrl?.trim() || null }
|
||
|
|
: {}),
|
||
|
|
...(dto.companyName !== undefined
|
||
|
|
? { companyName: dto.companyName.trim().slice(0, 200) }
|
||
|
|
: {}),
|
||
|
|
...(dto.companySubtitle !== undefined
|
||
|
|
? { companySubtitle: dto.companySubtitle.trim().slice(0, 200) }
|
||
|
|
: {}),
|
||
|
|
...(dto.address !== undefined
|
||
|
|
? { address: dto.address.trim().slice(0, 500) }
|
||
|
|
: {}),
|
||
|
|
...(dto.phone !== undefined
|
||
|
|
? { phone: dto.phone.trim().slice(0, 80) }
|
||
|
|
: {}),
|
||
|
|
...(dto.headerText !== undefined
|
||
|
|
? { headerText: dto.headerText.trim().slice(0, 1000) }
|
||
|
|
: {}),
|
||
|
|
...(dto.footerText !== undefined
|
||
|
|
? { footerText: dto.footerText.trim().slice(0, 1000) }
|
||
|
|
: {}),
|
||
|
|
};
|
||
|
|
|
||
|
|
const row = await this.prisma.tahesabInvoiceSettings.upsert({
|
||
|
|
where: { organizationId },
|
||
|
|
create: {
|
||
|
|
organizationId,
|
||
|
|
companyName: '',
|
||
|
|
...data,
|
||
|
|
},
|
||
|
|
update: data,
|
||
|
|
});
|
||
|
|
return toDto(row);
|
||
|
|
}
|
||
|
|
}
|