176 lines
3.6 KiB
TypeScript
176 lines
3.6 KiB
TypeScript
import { PrismaRepository } from '@gitroom/nestjs-libraries/database/prisma/prisma.service';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { Provider } from '@prisma/client';
|
|
import { AuthService } from '@gitroom/helpers/auth/auth.service';
|
|
import { UserDetailDto } from '@gitroom/nestjs-libraries/dtos/users/user.details.dto';
|
|
import { EmailNotificationsDto } from '@gitroom/nestjs-libraries/dtos/users/email-notifications.dto';
|
|
|
|
@Injectable()
|
|
export class UsersRepository {
|
|
constructor(private _user: PrismaRepository<'user'>) {}
|
|
|
|
getImpersonateUser(name: string) {
|
|
return this._user.model.user.findMany({
|
|
where: {
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: name,
|
|
},
|
|
},
|
|
{
|
|
email: {
|
|
contains: name,
|
|
},
|
|
},
|
|
{
|
|
id: {
|
|
contains: name,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
},
|
|
take: 10,
|
|
});
|
|
}
|
|
|
|
getUserById(id: string) {
|
|
return this._user.model.user.findFirst({
|
|
where: {
|
|
id,
|
|
},
|
|
});
|
|
}
|
|
|
|
getUserByEmail(email: string) {
|
|
return this._user.model.user.findFirst({
|
|
where: {
|
|
email,
|
|
providerName: Provider.LOCAL,
|
|
},
|
|
include: {
|
|
picture: {
|
|
select: {
|
|
id: true,
|
|
path: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
activateUser(id: string) {
|
|
return this._user.model.user.update({
|
|
where: {
|
|
id,
|
|
},
|
|
data: {
|
|
activated: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
getUserByProvider(providerId: string, provider: Provider) {
|
|
return this._user.model.user.findFirst({
|
|
where: {
|
|
providerId,
|
|
providerName: provider,
|
|
},
|
|
});
|
|
}
|
|
|
|
updatePassword(id: string, password: string) {
|
|
return this._user.model.user.update({
|
|
where: {
|
|
id,
|
|
providerName: Provider.LOCAL,
|
|
},
|
|
data: {
|
|
password: AuthService.hashPassword(password),
|
|
},
|
|
});
|
|
}
|
|
|
|
changeAudienceSize(userId: string, audience: number) {
|
|
return this._user.model.user.update({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
data: {
|
|
audience,
|
|
},
|
|
});
|
|
}
|
|
|
|
async getPersonal(userId: string) {
|
|
const user = await this._user.model.user.findUnique({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
bio: true,
|
|
picture: {
|
|
select: {
|
|
id: true,
|
|
path: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return user;
|
|
}
|
|
|
|
async changePersonal(userId: string, body: UserDetailDto) {
|
|
await this._user.model.user.update({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
data: {
|
|
name: body.fullname,
|
|
bio: body.bio,
|
|
picture: body.picture
|
|
? {
|
|
connect: {
|
|
id: body.picture.id,
|
|
},
|
|
}
|
|
: {
|
|
disconnect: true,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async getEmailNotifications(userId: string) {
|
|
return this._user.model.user.findUnique({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
sendSuccessEmails: true,
|
|
sendFailureEmails: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async updateEmailNotifications(userId: string, body: EmailNotificationsDto) {
|
|
await this._user.model.user.update({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
data: {
|
|
sendSuccessEmails: body.sendSuccessEmails,
|
|
sendFailureEmails: body.sendFailureEmails,
|
|
},
|
|
});
|
|
}
|
|
}
|