mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 13:25:12 +00:00
refactor(server): rename settings to user-settings (#11161)
This commit is contained in:
51
packages/backend/server/src/models/user-settings.ts
Normal file
51
packages/backend/server/src/models/user-settings.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Transactional } from '@nestjs-cls/transactional';
|
||||
import z from 'zod';
|
||||
|
||||
import { BaseModel } from './base';
|
||||
|
||||
export const UserSettingsSchema = z.object({
|
||||
receiveInvitationEmail: z.boolean().default(true),
|
||||
receiveMentionEmail: z.boolean().default(true),
|
||||
});
|
||||
|
||||
export type UserSettingsInput = z.input<typeof UserSettingsSchema>;
|
||||
export type UserSettings = z.infer<typeof UserSettingsSchema>;
|
||||
|
||||
/**
|
||||
* UserSettings Model
|
||||
*/
|
||||
@Injectable()
|
||||
export class UserSettingsModel extends BaseModel {
|
||||
@Transactional()
|
||||
async set(userId: string, setting: UserSettingsInput) {
|
||||
const existsSetting = await this.get(userId);
|
||||
const payload = UserSettingsSchema.parse({
|
||||
...existsSetting,
|
||||
...setting,
|
||||
});
|
||||
await this.db.userSettings.upsert({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
update: {
|
||||
payload,
|
||||
},
|
||||
create: {
|
||||
userId,
|
||||
payload,
|
||||
},
|
||||
});
|
||||
this.logger.log(`UserSettings updated for user ${userId}`);
|
||||
return payload;
|
||||
}
|
||||
|
||||
async get(userId: string): Promise<UserSettings> {
|
||||
const row = await this.db.userSettings.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
return UserSettingsSchema.parse(row?.payload ?? {});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user