Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(database): store a member's badge color in their workspace config (#22668)

Rdmclin2a82219140c04
diff --git a/packages/database/src/models/__tests__/workspaceMember.test.ts b/packages/database/src/models/__tests__/workspaceMember.test.ts
index c62bdc9dcf4..a7e2b29a54e 100644
--- a/packages/database/src/models/__tests__/workspaceMember.test.ts
+++ b/packages/database/src/models/__tests__/workspaceMember.test.ts
@@ -305,10 +305,11 @@ describe('WorkspaceMemberModel', () => {
expect(row.deletedAt).not.toBeNull();
});
- it('clears their job title and responsibilities so a re-invite starts without them', async () => {
+ it('clears their job title, responsibilities and config so a re-invite starts without them', async () => {
const model = new WorkspaceMemberModel(serverDB, inviterId);
await model.addMember({ userId: memberId, workspaceId });
await model.updateMemberProfile(workspaceId, memberId, {
+ badgeColor: '#1f8a5b',
title: 'Designer',
responsibilities: 'Design system',
});
@@ -318,6 +319,7 @@ describe('WorkspaceMemberModel', () => {
expect(revived.title).toBeNull();
expect(revived.responsibilities).toBeNull();
+ expect(revived.config).toEqual({});
});
it('drops their team memberships so a re-invite does not restore them', async () => {
@@ -593,6 +595,34 @@ describe('WorkspaceMemberModel', () => {
expect(cleared?.title).toBeNull();
// an omitted field is left untouched
expect(cleared?.responsibilities).toBe('Owns the mobile roadmap');
+ expect(cleared?.config).toEqual({});
+ });
+
+ it('keeps the badge color in config, leaving other config keys and fields alone', async () => {
+ const model = new WorkspaceMemberModel(serverDB, inviterId);
+ await model.addMember({ userId: memberId, workspaceId });
+ await serverDB
+ .update(workspaceMembers)
+ .set({ config: { other: 1 } as never })
+ .where(
+ and(eq(workspaceMembers.workspaceId, workspaceId), eq(workspaceMembers.userId, memberId)),
+ );
+
+ const colored = await model.updateMemberProfile(workspaceId, memberId, {
+ badgeColor: '#1f8a5b',
+ title: 'Designer',
+ });
+ expect(colored?.config).toEqual({ badgeColor: '#1f8a5b', other: 1 });
+ expect(colored?.title).toBe('Designer');
+
+ const recolored = await model.updateMemberProfile(workspaceId, memberId, {
+ badgeColor: '#123456',
+ });
+ expect(recolored?.config).toEqual({ badgeColor: '#123456', other: 1 });
+ expect(recolored?.title).toBe('Designer');
+
+ const reset = await model.updateMemberProfile(workspaceId, memberId, { badgeColor: null });
+ expect(reset?.config).toEqual({ other: 1 });
});
it('keeps the profile per workspace', async () => {
diff --git a/packages/database/src/models/workspaceMember.ts b/packages/database/src/models/workspaceMember.ts
index 6622712147b..b0a743d9bd4 100644
--- a/packages/database/src/models/workspaceMember.ts
+++ b/packages/database/src/models/workspaceMember.ts
@@ -213,8 +213,8 @@ export class WorkspaceMemberModel {
// membership) only means anything while they belong to the workspace, and
// the soft delete is reactivated by `addMember`, so a row left behind would
// silently restore their old access or teams on re-invite. Workspace-wide
- // rows carry no subject and stay. The job title and responsibilities are
- // cleared for the same reason: a re-invited member starts without them.
+ // rows carry no subject and stay. The job title, responsibilities and member
+ // config are cleared for the same reason: a re-invited member starts without them.
//
// Soft-delete first, then revoke, both in one transaction: the update takes
// the membership row lock that a concurrent grant waits on, so a grant can
@@ -223,7 +223,7 @@ export class WorkspaceMemberModel {
await this.db.transaction(async (tx) => {
await tx
.update(workspaceMembers)
- .set({ deletedAt: new Date(), title: null, responsibilities: null })
+ .set({ config: {}, deletedAt: new Date(), responsibilities: null, title: null })
.where(
and(
eq(workspaceMembers.workspaceId, workspaceId),
@@ -286,18 +286,31 @@ export class WorkspaceMemberModel {
};
/**
- * Set an active member's workspace-scoped job title and responsibilities.
- * `null` clears a field; an omitted field stays as it is. Returns the updated
- * row, or `undefined` when the user is not an active member.
+ * Set an active member's workspace-scoped job title, responsibilities and
+ * badge color (kept in `config`). `null` clears a field; an omitted field
+ * stays as it is, and other `config` keys are left alone. Returns the
+ * updated row, or `undefined` when the user is not an active member.
*/
updateMemberProfile = async (
workspaceId: string,
userId: string,
- profile: { title?: string | null; responsibilities?: string | null },
+ profile: {
+ badgeColor?: string | null;
+ responsibilities?: string | null;
+ title?: string | null;
+ },
) => {
+ const { badgeColor } = profile;
+ const config =
+ badgeColor === undefined
+ ? undefined
+ : badgeColor === null
+ ? sql`${workspaceMembers.config} - 'badgeColor'`
+ : sql`${workspaceMembers.config} || ${JSON.stringify({ badgeColor })}::jsonb`;
+
const [result] = await this.db
.update(workspaceMembers)
- .set({ title: profile.title, responsibilities: profile.responsibilities })
+ .set({ config, responsibilities: profile.responsibilities, title: profile.title })
.where(
and(
eq(workspaceMembers.workspaceId, workspaceId),