From b4ea88913c9b2faf133e1933f995b23b6ce5b00a 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 | 2 ++ front/lib/auth.ts | 7 +++++-- 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 +- .../pages/api/w/[wId]/members/[userId]/index.ts | 4 +++- front/pages/w/[wId]/assistant/new.tsx | 2 +- front/types/user.ts | 4 +++- 13 files changed, 40 insertions(+), 31 deletions(-) diff --git a/front/lib/api/assistant/conversation.ts b/front/lib/api/assistant/conversation.ts index a60913a50d8ae..32cffac3054c7 100644 --- a/front/lib/api/assistant/conversation.ts +++ b/front/lib/api/assistant/conversation.ts @@ -219,7 +219,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 b429226e928af..b3a69715b6d2a 100644 --- a/front/lib/api/assistant/global_agents.ts +++ b/front/lib/api/assistant/global_agents.ts @@ -66,7 +66,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 bd21395b65930..026a033eb9deb 100644 --- a/front/lib/api/workspace.ts +++ b/front/lib/api/workspace.ts @@ -79,6 +79,8 @@ export async function getMembers(auth: Authenticator): Promise { username: u.username, email: u.email, name: u.name, + 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 bbb4e5c475bdc..4b43241f4b5cf 100644 --- a/front/lib/auth.ts +++ b/front/lib/auth.ts @@ -268,7 +268,8 @@ export class Authenticator { providerId: this._user.providerId, username: this._user.username, email: this._user.email, - name: this._user.name, + firstName: this._user.firstName, + lastName: this._user.lastName || null, // Not available from this method image: null, workspaces: [], @@ -332,7 +333,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 124c4f0fc6de0..66e21bc36ef8b 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 79c1ebdee3e7c..405ad68a1ef1b 100644 --- a/front/pages/api/w/[wId]/assistant/conversations/index.ts +++ b/front/pages/api/w/[wId]/assistant/conversations/index.ts @@ -133,7 +133,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 243024e48ee7d..b2a99b13d03af 100644 --- a/front/pages/api/w/[wId]/members/[userId]/index.ts +++ b/front/pages/api/w/[wId]/members/[userId]/index.ts @@ -115,7 +115,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/w/[wId]/assistant/new.tsx b/front/pages/w/[wId]/assistant/new.tsx index 220f5d20c5d89..9e4c7a81446df 100644 --- a/front/pages/w/[wId]/assistant/new.tsx +++ b/front/pages/w/[wId]/assistant/new.tsx @@ -167,7 +167,7 @@ export default function AssistantNew({
{/* FEATURED AGENTS */} diff --git a/front/types/user.ts b/front/types/user.ts index 076280349d0c8..337fe504c6eaa 100644 --- a/front/types/user.ts +++ b/front/types/user.ts @@ -36,7 +36,9 @@ export type UserType = { providerId: string; username: string; email: string; - name: string; + firstName: string; + lastName: string | null; + fullName: string; image: string | null; workspaces: WorkspaceType[]; isDustSuperUser: boolean;