33 lines
1010 B
TypeScript
33 lines
1010 B
TypeScript
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
|
|
import { User } from '@prisma/client';
|
|
import { PrismaService } from '../../infrastructure/database/prisma.service';
|
|
|
|
@Injectable()
|
|
export class DefaultUserService implements OnApplicationBootstrap {
|
|
static readonly defaultOwnerSlug = 'default-owner';
|
|
static readonly defaultOwnerDisplayName = 'Default Owner';
|
|
|
|
constructor(private readonly prismaService: PrismaService) {}
|
|
|
|
async onApplicationBootstrap(): Promise<void> {
|
|
await this.getOrCreateDefaultUser();
|
|
}
|
|
|
|
async getOrCreateDefaultUser(): Promise<User> {
|
|
return this.prismaService.user.upsert({
|
|
where: {
|
|
slug: DefaultUserService.defaultOwnerSlug,
|
|
},
|
|
update: {
|
|
displayName: DefaultUserService.defaultOwnerDisplayName,
|
|
isDefault: true,
|
|
},
|
|
create: {
|
|
slug: DefaultUserService.defaultOwnerSlug,
|
|
displayName: DefaultUserService.defaultOwnerDisplayName,
|
|
isDefault: true,
|
|
},
|
|
});
|
|
}
|
|
}
|