From 4cfbc258d9ee2512aeeed1549e7b83c7febd9adf Mon Sep 17 00:00:00 2001 From: PopDaph Date: Wed, 18 Oct 2023 16:43:43 +0200 Subject: [PATCH] Make user first name mandatory and use accross the app --- front/lib/api/assistant/conversation.ts | 4 +++- front/lib/api/assistant/global_agents.ts | 2 +- front/lib/api/workspace.ts | 4 +++- front/lib/auth.ts | 10 ++++++-- front/lib/models/user.ts | 4 ++-- front/lib/user.ts | 17 +++++++------ .../20231017_user_first_and_last_name.ts | 17 +++++++------ .../[cId]/messages/[mId]/reactions/index.ts | 4 ++-- .../conversations/[cId]/messages/index.ts | 2 +- .../w/[wId]/assistant/conversations/index.ts | 2 +- .../api/w/[wId]/members/[userId]/index.ts | 4 +++- front/pages/poke/[wId]/memberships.tsx | 2 +- front/pages/w/[wId]/assistant/new.tsx | 2 +- front/pages/w/[wId]/members/index.tsx | 24 ++++++++++++------- front/pages/w/[wId]/welcome.tsx | 4 ++-- front/types/user.ts | 4 +++- 16 files changed, 62 insertions(+), 44 deletions(-) diff --git a/front/lib/api/assistant/conversation.ts b/front/lib/api/assistant/conversation.ts index 779272cdcf65a..3c94fc99b0447 100644 --- a/front/lib/api/assistant/conversation.ts +++ b/front/lib/api/assistant/conversation.ts @@ -221,7 +221,9 @@ async function renderUserMessage( providerId: user.providerId, username: user.username, email: user.email, - name: user.name, + firstName: user.firstName, + lastName: user.lastName, + fullName: user.firstName + (user.lastName ? ` ${user.lastName}` : ""), image: null, workspaces: [], isDustSuperUser: false, diff --git a/front/lib/api/assistant/global_agents.ts b/front/lib/api/assistant/global_agents.ts index 36984c3893f41..452895289c2d0 100644 --- a/front/lib/api/assistant/global_agents.ts +++ b/front/lib/api/assistant/global_agents.ts @@ -65,7 +65,7 @@ async function _getHelperGlobalAgent( const user = auth.user(); if (user) { const role = auth.role(); - prompt = `The user you're interacting with is granted with the role ${role}. Their name is ${user.name}. `; + prompt = `The user you're interacting with is granted with the role ${role}. Their name is ${user.fullName}. `; } const helperAssistantPromptInstance = HelperAssistantPrompt.getInstance(); diff --git a/front/lib/api/workspace.ts b/front/lib/api/workspace.ts index 15ea234c4e581..4d91a9c7697ac 100644 --- a/front/lib/api/workspace.ts +++ b/front/lib/api/workspace.ts @@ -76,7 +76,9 @@ export async function getMembers(auth: Authenticator): Promise { providerId: u.providerId, username: u.username, email: u.email, - name: u.name, + fullName: u.firstName + (u.lastName ? ` ${u.lastName}` : ""), + firstName: u.firstName, + lastName: u.lastName, image: null, workspaces: [{ ...owner, role }], isDustSuperUser: u.isDustSuperUser, diff --git a/front/lib/auth.ts b/front/lib/auth.ts index 373e8a77a81c2..c4ab4e126de4e 100644 --- a/front/lib/auth.ts +++ b/front/lib/auth.ts @@ -290,7 +290,11 @@ export class Authenticator { providerId: this._user.providerId, username: this._user.username, email: this._user.email, - name: this._user.name, + fullName: + this._user.firstName + + (this._user.lastName ? ` ${this._user.lastName}` : ""), + firstName: this._user.firstName, + lastName: this._user.lastName || null, // Not available from this method image: null, workspaces: [], @@ -354,7 +358,9 @@ export async function getUserFromSession( providerId: user.providerId, username: user.username, email: user.email, - name: user.name, + firstName: user.firstName, + lastName: user.lastName, + fullName: user.firstName + (user.lastName ? ` ${user.lastName}` : ""), image: session.user ? session.user.image : null, workspaces: workspaces.map((w) => { const m = memberships.find((m) => m.workspaceId === w.id); diff --git a/front/lib/models/user.ts b/front/lib/models/user.ts index 45fb4ff52545b..b1b445895cfc2 100644 --- a/front/lib/models/user.ts +++ b/front/lib/models/user.ts @@ -21,7 +21,7 @@ export class User extends Model< declare username: string; declare email: string; declare name: string; - declare firstName: string | null; + declare firstName: string; declare lastName: string | null; declare isDustSuperUser: CreationOptional; @@ -65,7 +65,7 @@ User.init( }, firstName: { type: DataTypes.STRING, - allowNull: true, + allowNull: false, }, lastName: { type: DataTypes.STRING, diff --git a/front/lib/user.ts b/front/lib/user.ts index 85396c6037c0a..d5488006471af 100644 --- a/front/lib/user.ts +++ b/front/lib/user.ts @@ -61,15 +61,14 @@ export function setUserMetadataFromClient(metadata: UserMetadataType) { export const guessFirstandLastNameFromFullName = ( fullName: string -): { firstName: string | null; lastName: string | null } => { - if (!fullName) return { firstName: null, lastName: null }; - +): { firstName: string; lastName: string | null } => { const nameParts = fullName.split(" "); - if (nameParts.length === 1) return { firstName: fullName, lastName: null }; - - const firstName = nameParts.shift() || null; - const lastName = nameParts.join(" "); - - return { firstName, lastName }; + if (nameParts.length > 1) { + const firstName = nameParts.shift() || fullName; + const lastName = nameParts.join(" "); + return { firstName, lastName }; + } else { + return { firstName: fullName, lastName: null }; + } }; diff --git a/front/migrations/20231017_user_first_and_last_name.ts b/front/migrations/20231017_user_first_and_last_name.ts index dc6c1632f19d8..2665976a0f60a 100644 --- a/front/migrations/20231017_user_first_and_last_name.ts +++ b/front/migrations/20231017_user_first_and_last_name.ts @@ -1,15 +1,14 @@ -import { Op } from "sequelize"; - import { User } from "@app/lib/models"; import { guessFirstandLastNameFromFullName } from "@app/lib/user"; async function main() { - const users = await User.findAll({ - where: { - firstName: { - [Op.or]: [null, ""], - }, - }, + const users: User[] = await User.findAll({ + // Was run with this were but then we make first name non nullable and linter is not happy + // where: { + // firstName: { + // [Op.or]: [null, ""], + // }, + // }, }); console.log(`Found ${users.length} users to update`); @@ -23,7 +22,7 @@ async function main() { console.log(`Processing chunk ${i}/${chunks.length}...`); const chunk = chunks[i]; await Promise.all( - chunk.map((u) => { + chunk.map((u: User) => { return (async () => { if (!u.firstName) { const { firstName, lastName } = guessFirstandLastNameFromFullName( diff --git a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/reactions/index.ts b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/reactions/index.ts index 4945702ed8b2a..5ad9f93a6077c 100644 --- a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/reactions/index.ts +++ b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/reactions/index.ts @@ -118,7 +118,7 @@ async function handler( user: user, context: { username: user.username, - fullName: user.name, + fullName: user.fullName, }, reaction: bodyValidation.right.reaction, }); @@ -142,7 +142,7 @@ async function handler( user: user, context: { username: user.username, - fullName: user.name, + fullName: user.fullName, }, reaction: bodyValidation.right.reaction, }); diff --git a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts index 363bf20010186..7dc998b53859c 100644 --- a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts +++ b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts @@ -118,7 +118,7 @@ async function handler( context: { timezone: context.timezone, username: user.username, - fullName: user.name, + fullName: user.fullName, email: user.email, profilePictureUrl: context.profilePictureUrl, }, diff --git a/front/pages/api/w/[wId]/assistant/conversations/index.ts b/front/pages/api/w/[wId]/assistant/conversations/index.ts index e3e84f62dad14..12fc00440ede4 100644 --- a/front/pages/api/w/[wId]/assistant/conversations/index.ts +++ b/front/pages/api/w/[wId]/assistant/conversations/index.ts @@ -138,7 +138,7 @@ async function handler( context: { timezone: message.context.timezone, username: user.username, - fullName: user.name, + fullName: user.fullName, email: user.email, profilePictureUrl: message.context.profilePictureUrl, }, diff --git a/front/pages/api/w/[wId]/members/[userId]/index.ts b/front/pages/api/w/[wId]/members/[userId]/index.ts index 724ace4d2e9e5..9a2750d0734ef 100644 --- a/front/pages/api/w/[wId]/members/[userId]/index.ts +++ b/front/pages/api/w/[wId]/members/[userId]/index.ts @@ -121,7 +121,9 @@ async function handler( providerId: user.providerId, username: user.username, email: user.email, - name: user.name, + firstName: user.firstName, + lastName: user.lastName, + fullName: user.firstName + (user.lastName ? ` ${user.lastName}` : ""), image: null, workspaces: [w], isDustSuperUser: user.isDustSuperUser, diff --git a/front/pages/poke/[wId]/memberships.tsx b/front/pages/poke/[wId]/memberships.tsx index 78fdfa767df27..94de9c2767773 100644 --- a/front/pages/poke/[wId]/memberships.tsx +++ b/front/pages/poke/[wId]/memberships.tsx @@ -105,7 +105,7 @@ const MembershipsPage = ({ >

- {m.username} ({m.name}) + {m.username} ({m.fullName})

diff --git a/front/pages/w/[wId]/assistant/new.tsx b/front/pages/w/[wId]/assistant/new.tsx index 976c86012f7a1..7d5df029e00a0 100644 --- a/front/pages/w/[wId]/assistant/new.tsx +++ b/front/pages/w/[wId]/assistant/new.tsx @@ -183,7 +183,7 @@ export default function AssistantNew({

{/* FEATURED AGENTS */} diff --git a/front/pages/w/[wId]/members/index.tsx b/front/pages/w/[wId]/members/index.tsx index b1a7550bebca1..1b2403ec40235 100644 --- a/front/pages/w/[wId]/members/index.tsx +++ b/front/pages/w/[wId]/members/index.tsx @@ -189,12 +189,12 @@ export default function WorkspaceAdmin({ | MembershipInvitationType )[] = [ ...members - .sort((a, b) => a.name.localeCompare(b.name)) + .sort((a, b) => a.fullName.localeCompare(b.fullName)) .filter((m) => m.workspaces[0].role !== "none") .filter( (m) => !searchText || - m.name.toLowerCase().includes(searchText.toLowerCase()) || + m.fullName.toLowerCase().includes(searchText.toLowerCase()) || m.email?.toLowerCase().includes(searchText.toLowerCase()) || m.username?.toLowerCase().includes(searchText.toLowerCase()) ), @@ -275,13 +275,17 @@ export default function WorkspaceAdmin({ {isInvitation(item) ? ( ) : ( - + )}
{!isInvitation(item) && (
- {item.name} + {item.fullName} {user?.id === item.id && " (you)"}
)} @@ -650,7 +654,7 @@ function ChangeMemberModal({ sendNotification({ type: "success", title: "Role updated", - description: `Role updated for ${member.name}.`, + description: `Role updated for ${member.fullName}.`, }); await mutate(`/api/w/${owner.sId}/members`); } @@ -670,7 +674,7 @@ function ChangeMemberModal({ hasChanged={ selectedRole !== null && selectedRole !== member.workspaces[0].role } - title={member.name || "Unreachable"} + title={member.fullName || "Unreachable"} type="right-side" onSave={async () => { setIsSaving(true); @@ -687,9 +691,11 @@ function ChangeMemberModal({ >
- +
-
{member.name}
+
+ {member.fullName} +
{member.email}
@@ -747,7 +753,7 @@ function ChangeMemberModal({
Revoke access for user{" "} - {member.name}? + {member.fullName}?