website refactor
This commit is contained in:
13
apps/website/lib/services/sponsors/SponsorService.ts
Normal file
13
apps/website/lib/services/sponsors/SponsorService.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Sponsor Service - DTO Only
|
||||
*
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
* All client-side presentation logic must be handled by hooks/components.
|
||||
*/
|
||||
export class SponsorService {
|
||||
constructor(private readonly apiClient: any) {}
|
||||
|
||||
async getSponsorById(sponsorId: string): Promise<any> {
|
||||
return { id: sponsorId, name: 'Sponsor' };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { isProductionEnvironment } from '@/lib/config/env';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import type { Service } from '@/lib/contracts/services/Service';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
|
||||
export interface PendingSponsorshipRequestDto {
|
||||
requestId: string;
|
||||
sponsorId: string;
|
||||
sponsorName: string;
|
||||
message: string | null;
|
||||
createdAtIso: string;
|
||||
}
|
||||
|
||||
export interface SponsorshipRequestsReadApiDto {
|
||||
sections: Array<{
|
||||
entityType: 'driver' | 'team' | 'season';
|
||||
entityId: string;
|
||||
entityName: string;
|
||||
requests: PendingSponsorshipRequestDto[];
|
||||
}>;
|
||||
}
|
||||
|
||||
export type SponsorshipRequestsReadServiceError = 'notFound' | 'unauthorized' | 'serverError';
|
||||
|
||||
export class SponsorshipRequestsReadService implements Service {
|
||||
private readonly client: SponsorsApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: true,
|
||||
logToConsole: true,
|
||||
reportToExternal: isProductionEnvironment(),
|
||||
});
|
||||
|
||||
this.client = new SponsorsApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
|
||||
async getPendingRequestsForDriver(
|
||||
driverId: string,
|
||||
): Promise<Result<SponsorshipRequestsReadApiDto, SponsorshipRequestsReadServiceError>> {
|
||||
try {
|
||||
const response = await this.client.getPendingSponsorshipRequests({
|
||||
entityType: 'driver',
|
||||
entityId: driverId,
|
||||
});
|
||||
|
||||
const requests = (response.requests ?? []).map((r) => {
|
||||
const raw = r as unknown as {
|
||||
id?: string;
|
||||
requestId?: string;
|
||||
sponsorId?: string;
|
||||
sponsorName?: string;
|
||||
message?: unknown;
|
||||
createdAt?: string;
|
||||
createdAtIso?: string;
|
||||
};
|
||||
|
||||
return {
|
||||
requestId: String(raw.id ?? raw.requestId ?? ''),
|
||||
sponsorId: String(raw.sponsorId ?? ''),
|
||||
sponsorName: String(raw.sponsorName ?? 'Sponsor'),
|
||||
message: typeof raw.message === 'string' ? raw.message : null,
|
||||
createdAtIso: String(raw.createdAt ?? raw.createdAtIso ?? ''),
|
||||
};
|
||||
});
|
||||
|
||||
return Result.ok({
|
||||
sections: [
|
||||
{
|
||||
entityType: 'driver',
|
||||
entityId: driverId,
|
||||
entityName: 'Your Profile',
|
||||
requests,
|
||||
},
|
||||
],
|
||||
});
|
||||
} catch (error) {
|
||||
const errorAny = error as { statusCode?: number; message?: string };
|
||||
if (errorAny.statusCode === 401) return Result.err('unauthorized');
|
||||
if (errorAny.statusCode === 404) return Result.err('notFound');
|
||||
return Result.err('serverError');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { isProductionEnvironment } from '@/lib/config/env';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import type { Service } from '@/lib/contracts/services/Service';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
|
||||
export interface AcceptSponsorshipRequestCommand {
|
||||
requestId: string;
|
||||
actorDriverId: string;
|
||||
}
|
||||
|
||||
export interface RejectSponsorshipRequestCommand {
|
||||
requestId: string;
|
||||
actorDriverId: string;
|
||||
reason: string | null;
|
||||
}
|
||||
|
||||
export type AcceptSponsorshipRequestServiceError =
|
||||
| 'ACCEPT_SPONSORSHIP_REQUEST_FAILED';
|
||||
|
||||
export type RejectSponsorshipRequestServiceError =
|
||||
| 'REJECT_SPONSORSHIP_REQUEST_FAILED';
|
||||
|
||||
export class SponsorshipRequestsService implements Service {
|
||||
private readonly client: SponsorsApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: true,
|
||||
logToConsole: true,
|
||||
reportToExternal: isProductionEnvironment(),
|
||||
});
|
||||
|
||||
this.client = new SponsorsApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
|
||||
async acceptRequest(
|
||||
command: AcceptSponsorshipRequestCommand,
|
||||
): Promise<Result<void, AcceptSponsorshipRequestServiceError>> {
|
||||
try {
|
||||
await this.client.acceptSponsorshipRequest(command.requestId, {
|
||||
actorDriverId: command.actorDriverId,
|
||||
} as any);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch {
|
||||
return Result.err('ACCEPT_SPONSORSHIP_REQUEST_FAILED');
|
||||
}
|
||||
}
|
||||
|
||||
async rejectRequest(
|
||||
command: RejectSponsorshipRequestCommand,
|
||||
): Promise<Result<void, RejectSponsorshipRequestServiceError>> {
|
||||
try {
|
||||
await this.client.rejectSponsorshipRequest(command.requestId, {
|
||||
actorDriverId: command.actorDriverId,
|
||||
reason: command.reason ?? undefined,
|
||||
} as any);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch {
|
||||
return Result.err('REJECT_SPONSORSHIP_REQUEST_FAILED');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user