diff --git a/server/models/user/user.ts b/server/models/user/user.ts index db5371c8..635d6806 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts @@ -100,6 +100,12 @@ export class User extends TimeStamps implements Base { }) type: UserType; + /** + * 用户的额外信息 + */ + @prop() + extra?: object; + /** * 用户设置 */ diff --git a/server/services/core/user/user.service.ts b/server/services/core/user/user.service.ts index 0b57c449..00f847ba 100644 --- a/server/services/core/user/user.service.ts +++ b/server/services/core/user/user.service.ts @@ -45,10 +45,12 @@ class UserService extends TcService { '_id', 'username', 'email', - 'avatar', 'nickname', 'discriminator', 'temporary', + 'avatar', + 'type', + 'extra', 'createdAt', ]); @@ -157,6 +159,12 @@ class UserService extends TcService { fieldValue: 'any', }, }); + this.registerAction('updateUserExtra', this.updateUserExtra, { + params: { + fieldName: 'string', + fieldValue: 'any', + }, + }); this.registerAction('getUserSettings', this.getUserSettings); this.registerAction('setUserSettings', this.setUserSettings, { params: { @@ -546,6 +554,9 @@ class UserService extends TcService { return list; } + /** + * 修改用户字段 + */ async updateUserField( ctx: TcContext<{ fieldName: string; fieldValue: string }> ) { @@ -553,7 +564,8 @@ class UserService extends TcService { const t = ctx.meta.t; const userId = ctx.meta.userId; if (!['nickname', 'avatar'].includes(fieldName)) { - throw new EntityError(t('该数据不允许修改')); + // 只允许修改以上字段 + throw new EntityError(`${t('该数据不允许修改')}: ${fieldName}`); } const doc = await this.adapter.model @@ -575,6 +587,34 @@ class UserService extends TcService { return await this.transformDocuments(ctx, {}, doc); } + /** + * 修改用户额外数据 + */ + async updateUserExtra( + ctx: TcContext<{ fieldName: string; fieldValue: string }> + ) { + const { fieldName, fieldValue } = ctx.params; + const userId = ctx.meta.userId; + + const doc = await this.adapter.model + .findOneAndUpdate( + { + _id: new Types.ObjectId(userId), + }, + { + [`extra.${fieldName}`]: fieldValue, + }, + { + new: true, + } + ) + .exec(); + + this.cleanCurrentUserCache(ctx); + + return await this.transformDocuments(ctx, {}, doc); + } + /** * 获取用户个人配置 */ diff --git a/server/test/integration/user/user.spec.ts b/server/test/integration/user/user.spec.ts index 84926bf4..f328803a 100644 --- a/server/test/integration/user/user.spec.ts +++ b/server/test/integration/user/user.spec.ts @@ -103,6 +103,48 @@ describe('Test "user" service', () => { expect(res).not.toHaveProperty('password'); }); + test('Test "user.updateUserExtra"', async () => { + const testUser = await insertTestData(createTestUser()); + + const res = await broker.call( + 'user.updateUserExtra', + { + fieldName: 'foo', + fieldValue: 'bar', + }, + { + meta: { + userId: String(testUser._id), + }, + } + ); + + expect(res).toMatchObject({ + extra: { + foo: 'bar', + }, + }); + + const res2 = await broker.call( + 'user.updateUserExtra', + { + fieldName: 'foo', + fieldValue: 'baz', + }, + { + meta: { + userId: String(testUser._id), + }, + } + ); + + expect(res2).toMatchObject({ + extra: { + foo: 'baz', + }, + }); + }); + test('Test "user.setUserSettings"', async () => { const testUser = await insertTestData(createTestUser());