66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
|
|
import { SignupWithEmailUseCase } from '@core/identity/application/use-cases/SignupWithEmailUseCase';
|
|
import { CreateAchievementUseCase } from '@core/identity/application/use-cases/achievement/CreateAchievementUseCase';
|
|
import type { Logger } from '@core/shared/application';
|
|
|
|
import {
|
|
DRIVER_ACHIEVEMENTS,
|
|
STEWARD_ACHIEVEMENTS,
|
|
ADMIN_ACHIEVEMENTS,
|
|
COMMUNITY_ACHIEVEMENTS,
|
|
} from '@core/identity/domain/AchievementConstants';
|
|
|
|
|
|
export class EnsureInitialData {
|
|
constructor(
|
|
private readonly signupUseCase: SignupWithEmailUseCase,
|
|
private readonly createAchievementUseCase: CreateAchievementUseCase,
|
|
private readonly logger: Logger,
|
|
) {}
|
|
|
|
async execute(): Promise<void> {
|
|
// Ensure initial admin user exists
|
|
const signupResult = await this.signupUseCase.execute({
|
|
email: 'admin@gridpilot.local',
|
|
password: 'admin123',
|
|
displayName: 'Admin',
|
|
});
|
|
|
|
if (signupResult.isOk()) {
|
|
this.logger.info('[Bootstrap] Initial admin user created');
|
|
} else if (signupResult.error?.code === 'EMAIL_ALREADY_EXISTS') {
|
|
this.logger.info('[Bootstrap] Admin user already exists');
|
|
} else {
|
|
const detailsMessage =
|
|
signupResult.error && typeof signupResult.error === 'object' && 'details' in signupResult.error
|
|
? (signupResult.error as { details?: { message?: string } }).details?.message
|
|
: undefined;
|
|
|
|
throw new Error(detailsMessage ?? 'Failed to ensure initial admin user');
|
|
}
|
|
|
|
// Ensure initial achievements exist
|
|
const allAchievements = [
|
|
...DRIVER_ACHIEVEMENTS,
|
|
...STEWARD_ACHIEVEMENTS,
|
|
...ADMIN_ACHIEVEMENTS,
|
|
...COMMUNITY_ACHIEVEMENTS,
|
|
];
|
|
|
|
let createdCount = 0;
|
|
let existingCount = 0;
|
|
|
|
for (const achievementProps of allAchievements) {
|
|
try {
|
|
await this.createAchievementUseCase.execute(achievementProps);
|
|
createdCount++;
|
|
} catch {
|
|
// If achievement already exists, that's fine
|
|
existingCount++;
|
|
}
|
|
}
|
|
|
|
this.logger.info(`[Bootstrap] Achievements: ${createdCount} created, ${existingCount} already exist`);
|
|
}
|
|
}
|