mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
refactor(server): rename settings to user-settings (#11161)
This commit is contained in:
@@ -43,7 +43,7 @@ test.after(async t => {
|
||||
});
|
||||
|
||||
test('should get a user settings with default value', async t => {
|
||||
const settings = await t.context.models.settings.get(user.id);
|
||||
const settings = await t.context.models.userSettings.get(user.id);
|
||||
t.deepEqual(settings, {
|
||||
receiveInvitationEmail: true,
|
||||
receiveMentionEmail: true,
|
||||
@@ -51,28 +51,28 @@ test('should get a user settings with default value', async t => {
|
||||
});
|
||||
|
||||
test('should update a user settings', async t => {
|
||||
const settings = await t.context.models.settings.set(user.id, {
|
||||
const settings = await t.context.models.userSettings.set(user.id, {
|
||||
receiveInvitationEmail: false,
|
||||
});
|
||||
t.deepEqual(settings, {
|
||||
receiveInvitationEmail: false,
|
||||
receiveMentionEmail: true,
|
||||
});
|
||||
const settings2 = await t.context.models.settings.get(user.id);
|
||||
const settings2 = await t.context.models.userSettings.get(user.id);
|
||||
t.deepEqual(settings2, settings);
|
||||
|
||||
// update existing setting
|
||||
const setting3 = await t.context.models.settings.set(user.id, {
|
||||
const setting3 = await t.context.models.userSettings.set(user.id, {
|
||||
receiveInvitationEmail: true,
|
||||
});
|
||||
t.deepEqual(setting3, {
|
||||
receiveInvitationEmail: true,
|
||||
receiveMentionEmail: true,
|
||||
});
|
||||
const setting4 = await t.context.models.settings.get(user.id);
|
||||
const setting4 = await t.context.models.userSettings.get(user.id);
|
||||
t.deepEqual(setting4, setting3);
|
||||
|
||||
const setting5 = await t.context.models.settings.set(user.id, {
|
||||
const setting5 = await t.context.models.userSettings.set(user.id, {
|
||||
receiveMentionEmail: false,
|
||||
receiveInvitationEmail: false,
|
||||
});
|
||||
@@ -80,13 +80,13 @@ test('should update a user settings', async t => {
|
||||
receiveInvitationEmail: false,
|
||||
receiveMentionEmail: false,
|
||||
});
|
||||
const setting6 = await t.context.models.settings.get(user.id);
|
||||
const setting6 = await t.context.models.userSettings.get(user.id);
|
||||
t.deepEqual(setting6, setting5);
|
||||
});
|
||||
|
||||
test('should throw error when update settings with invalid payload', async t => {
|
||||
await t.throwsAsync(
|
||||
t.context.models.settings.set(user.id, {
|
||||
t.context.models.userSettings.set(user.id, {
|
||||
// @ts-expect-error invalid setting input types
|
||||
receiveInvitationEmail: 1,
|
||||
}),
|
||||
@@ -17,10 +17,10 @@ import { HistoryModel } from './history';
|
||||
import { NotificationModel } from './notification';
|
||||
import { MODELS_SYMBOL } from './provider';
|
||||
import { SessionModel } from './session';
|
||||
import { SettingsModel } from './settings';
|
||||
import { UserModel } from './user';
|
||||
import { UserDocModel } from './user-doc';
|
||||
import { UserFeatureModel } from './user-feature';
|
||||
import { UserSettingsModel } from './user-settings';
|
||||
import { VerificationTokenModel } from './verification-token';
|
||||
import { WorkspaceModel } from './workspace';
|
||||
import { WorkspaceFeatureModel } from './workspace-feature';
|
||||
@@ -40,7 +40,7 @@ const MODELS = {
|
||||
docUser: DocUserModel,
|
||||
history: HistoryModel,
|
||||
notification: NotificationModel,
|
||||
settings: SettingsModel,
|
||||
userSettings: UserSettingsModel,
|
||||
copilotSession: CopilotSessionModel,
|
||||
copilotContext: CopilotContextModel,
|
||||
copilotJob: CopilotJobModel,
|
||||
@@ -104,10 +104,10 @@ export * from './feature';
|
||||
export * from './history';
|
||||
export * from './notification';
|
||||
export * from './session';
|
||||
export * from './settings';
|
||||
export * from './user';
|
||||
export * from './user-doc';
|
||||
export * from './user-feature';
|
||||
export * from './user-settings';
|
||||
export * from './verification-token';
|
||||
export * from './workspace';
|
||||
export * from './workspace-feature';
|
||||
|
||||
@@ -4,27 +4,27 @@ import z from 'zod';
|
||||
|
||||
import { BaseModel } from './base';
|
||||
|
||||
export const SettingsSchema = z.object({
|
||||
export const UserSettingsSchema = z.object({
|
||||
receiveInvitationEmail: z.boolean().default(true),
|
||||
receiveMentionEmail: z.boolean().default(true),
|
||||
});
|
||||
|
||||
export type SettingsInput = z.input<typeof SettingsSchema>;
|
||||
export type Settings = z.infer<typeof SettingsSchema>;
|
||||
export type UserSettingsInput = z.input<typeof UserSettingsSchema>;
|
||||
export type UserSettings = z.infer<typeof UserSettingsSchema>;
|
||||
|
||||
/**
|
||||
* Settings Model
|
||||
* UserSettings Model
|
||||
*/
|
||||
@Injectable()
|
||||
export class SettingsModel extends BaseModel {
|
||||
export class UserSettingsModel extends BaseModel {
|
||||
@Transactional()
|
||||
async set(userId: string, setting: SettingsInput) {
|
||||
async set(userId: string, setting: UserSettingsInput) {
|
||||
const existsSetting = await this.get(userId);
|
||||
const payload = SettingsSchema.parse({
|
||||
const payload = UserSettingsSchema.parse({
|
||||
...existsSetting,
|
||||
...setting,
|
||||
});
|
||||
await this.db.settings.upsert({
|
||||
await this.db.userSettings.upsert({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
@@ -36,16 +36,16 @@ export class SettingsModel extends BaseModel {
|
||||
payload,
|
||||
},
|
||||
});
|
||||
this.logger.log(`Settings updated for user ${userId}`);
|
||||
this.logger.log(`UserSettings updated for user ${userId}`);
|
||||
return payload;
|
||||
}
|
||||
|
||||
async get(userId: string): Promise<Settings> {
|
||||
const row = await this.db.settings.findUnique({
|
||||
async get(userId: string): Promise<UserSettings> {
|
||||
const row = await this.db.userSettings.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
return SettingsSchema.parse(row?.payload ?? {});
|
||||
return UserSettingsSchema.parse(row?.payload ?? {});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user