games_app/backend/src/services/advertisementService.ts
2026-06-29 15:01:23 +02:00

105 lines
4.1 KiB
TypeScript

import { AdPlacement, AdvertisementDto, AdvertisementRow } from '../models/advertisement.model';
import * as advertisementRepository from '../repositories/advertisementRepository';
const VALID_PLACEMENTS: AdPlacement[] = ['home_hero', 'home_strip', 'home_sidebar'];
function toAdDto(row: AdvertisementRow): AdvertisementDto {
return {
id: row.id,
placement: row.placement,
title: row.title,
subtitle: row.subtitle,
ctaLabel: row.cta_label,
linkUrl: row.link_url,
imageUrl: row.image_url,
backgroundGradient: row.background_gradient,
accentColor: row.accent_color ?? '#7C5CFF',
isActive: row.is_active,
sortOrder: row.sort_order,
startsAt: row.starts_at ? row.starts_at.toISOString() : null,
endsAt: row.ends_at ? row.ends_at.toISOString() : null,
createdAt: row.created_at.toISOString(),
updatedAt: row.updated_at.toISOString(),
};
}
export async function listAdvertisements(placement?: string): Promise<AdvertisementDto[]> {
const p =
placement && VALID_PLACEMENTS.includes(placement as AdPlacement)
? (placement as AdPlacement)
: undefined;
const rows = await advertisementRepository.listActiveAdvertisements(p);
return rows.map(toAdDto);
}
export async function listAllAdvertisementsAdmin(): Promise<AdvertisementDto[]> {
const rows = await advertisementRepository.listAllAdvertisements();
return rows.map(toAdDto);
}
export function parseAdvertisementInput(body: Record<string, unknown>, partial = false) {
const title = typeof body.title === 'string' ? body.title.trim() : '';
if (!partial && !title) throw new Error('title is required');
const placement =
typeof body.placement === 'string' && VALID_PLACEMENTS.includes(body.placement as AdPlacement)
? (body.placement as AdPlacement)
: undefined;
return {
placement,
title: title || undefined,
subtitle: typeof body.subtitle === 'string' ? body.subtitle : body.subtitle === null ? null : undefined,
ctaLabel: typeof body.ctaLabel === 'string' ? body.ctaLabel : body.ctaLabel === null ? null : undefined,
linkUrl: typeof body.linkUrl === 'string' ? body.linkUrl : body.linkUrl === null ? null : undefined,
imageUrl: typeof body.imageUrl === 'string' ? body.imageUrl : body.imageUrl === null ? null : undefined,
backgroundGradient:
typeof body.backgroundGradient === 'string'
? body.backgroundGradient
: body.backgroundGradient === null
? null
: undefined,
accentColor: typeof body.accentColor === 'string' ? body.accentColor : undefined,
isActive: typeof body.isActive === 'boolean' ? body.isActive : undefined,
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
startsAt:
typeof body.startsAt === 'string' ? body.startsAt : body.startsAt === null ? null : undefined,
endsAt: typeof body.endsAt === 'string' ? body.endsAt : body.endsAt === null ? null : undefined,
};
}
export async function createAdvertisement(body: Record<string, unknown>): Promise<AdvertisementDto> {
const input = parseAdvertisementInput(body);
if (!input.title) throw new Error('title is required');
const row = await advertisementRepository.createAdvertisement({
placement: input.placement,
title: input.title,
subtitle: input.subtitle,
ctaLabel: input.ctaLabel,
linkUrl: input.linkUrl,
imageUrl: input.imageUrl,
backgroundGradient: input.backgroundGradient,
accentColor: input.accentColor,
isActive: input.isActive,
sortOrder: input.sortOrder,
startsAt: input.startsAt,
endsAt: input.endsAt,
});
return toAdDto(row);
}
export async function updateAdvertisement(
id: string,
body: Record<string, unknown>
): Promise<AdvertisementDto> {
const input = parseAdvertisementInput(body, true);
const row = await advertisementRepository.updateAdvertisement(id, input);
if (!row) throw new Error('Advertisement not found');
return toAdDto(row);
}
export async function deleteAdvertisement(id: string): Promise<void> {
const ok = await advertisementRepository.deleteAdvertisement(id);
if (!ok) throw new Error('Advertisement not found');
}