Skip to content

Commit

Permalink
wip: user service
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 6, 2024
1 parent 2a4139b commit 7628fa5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/database/server/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class UserModel {
return this.db.delete(userSettings).where(eq(userSettings.id, this.userId));
}

async updateSetting(value: Partial<UserSettings>) {
async updateSetting(value: DeepPartial<UserSettings>) {
const { keyVaults, ...res } = value;

// Encrypt keyVaults
Expand Down
26 changes: 7 additions & 19 deletions src/services/user/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ import { DeepPartial } from 'utility-types';
import { Mock, beforeEach, describe, expect, it, vi } from 'vitest';

import { UserModel } from '@/database/_deprecated/models/user';
import { migrate } from '@/database/client/migrate';
import { UserPreference } from '@/types/user';
import { UserSettings } from '@/types/user/settings';
import { AsyncLocalStorage } from '@/utils/localStorage';

import { ClientService } from './client';

vi.mock('@/database/_deprecated/models/user', () => ({
UserModel: {
getUser: vi.fn(),
updateSettings: vi.fn(),
resetSettings: vi.fn(),
updateAvatar: vi.fn(),
},
}));

const mockUser = {
avatar: 'avatar.png',
settings: { themeMode: 'light' } as unknown as UserSettings,
Expand All @@ -26,15 +18,15 @@ const mockUser = {
const mockPreference = {
useCmdEnterToSend: true,
} as UserPreference;
const clientService = new ClientService('123');

describe('ClientService', () => {
let clientService: ClientService;
beforeEach(async () => {
vi.clearAllMocks();

beforeEach(() => {
vi.clearAllMocks();
clientService = new ClientService();
});
await migrate();
});

describe('ClientService', () => {
it('should get user state correctly', async () => {
(UserModel.getUser as Mock).mockResolvedValue(mockUser);
const spyOn = vi
Expand Down Expand Up @@ -77,12 +69,8 @@ describe('ClientService', () => {

it('should update user avatar correctly', async () => {
const newAvatar = 'new-avatar.png';
(UserModel.updateAvatar as Mock).mockResolvedValue(undefined);

await clientService.updateAvatar(newAvatar);

expect(UserModel.updateAvatar).toHaveBeenCalledWith(newAvatar);
expect(UserModel.updateAvatar).toHaveBeenCalledTimes(1);
});

it('should update user preference correctly', async () => {
Expand Down
34 changes: 19 additions & 15 deletions src/services/user/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DeepPartial } from 'utility-types';

import { MessageModel } from '@/database/_deprecated/models/message';
import { SessionModel } from '@/database/_deprecated/models/session';
import { UserModel } from '@/database/_deprecated/models/user';
import { clientDB } from '@/database/client/db';
import { MessageModel } from '@/database/server/models/message';
import { SessionModel } from '@/database/server/models/session';
import { UserModel } from '@/database/server/models/user';
import { UserGuide, UserInitializationState, UserPreference } from '@/types/user';
import { UserSettings } from '@/types/user/settings';
import { AsyncLocalStorage } from '@/utils/localStorage';
Expand All @@ -11,39 +12,42 @@ import { IUserService } from './type';

export class ClientService implements IUserService {
private preferenceStorage: AsyncLocalStorage<UserPreference>;
private userModel: UserModel;
private messageModel: MessageModel;
private sessionModel: SessionModel;

constructor() {
constructor(userId: string) {
this.preferenceStorage = new AsyncLocalStorage('LOBE_PREFERENCE');
this.userModel = new UserModel(clientDB as any, userId);
this.messageModel = new MessageModel(clientDB as any, userId);
this.sessionModel = new SessionModel(clientDB as any, userId);
}

async getUserState(): Promise<UserInitializationState> {
const user = await UserModel.getUser();
const messageCount = await MessageModel.count();
const sessionCount = await SessionModel.count();
const user = await this.userModel.getUserState();
const messageCount = await this.messageModel.count();
const sessionCount = await this.sessionModel.count();

return {
avatar: user.avatar,
...user,
canEnablePWAGuide: messageCount >= 4,
canEnableTrace: messageCount >= 4,
hasConversation: messageCount > 0 || sessionCount > 0,
isOnboard: true,
preference: await this.preferenceStorage.getFromLocalStorage(),
settings: user.settings as UserSettings,
userId: user.uuid,
};
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
updateUserSettings = async (patch: DeepPartial<UserSettings>, _?: any) => {
return UserModel.updateSettings(patch);
updateUserSettings = async (patch: DeepPartial<UserSettings>) => {
return this.userModel.updateSetting(patch);
};

resetUserSettings = async () => {
return UserModel.resetSettings();
return this.userModel.deleteSetting();
};

updateAvatar(avatar: string) {
return UserModel.updateAvatar(avatar);
return this.userModel.updateUser({ avatar });
}

async updatePreference(preference: Partial<UserPreference>) {
Expand Down

0 comments on commit 7628fa5

Please sign in to comment.