Skip to content

Commit

Permalink
Make user first name mandatory and use accross the app
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Oct 18, 2023
1 parent be0e86b commit 12f7ac4
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 31 deletions.
3 changes: 2 additions & 1 deletion front/lib/api/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ async function renderUserMessage(
providerId: user.providerId,
username: user.username,
email: user.email,
name: user.name,
firstName: user.firstName,
lastName: user.lastName,
image: null,
workspaces: [],
isDustSuperUser: false,
Expand Down
2 changes: 1 addition & 1 deletion front/lib/api/assistant/global_agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions front/lib/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export async function getMembers(auth: Authenticator): Promise<UserType[]> {
username: u.username,
email: u.email,
name: u.name,
firstName: u.firstName,
lastName: u.lastName,
image: null,
workspaces: [{ ...owner, role }],
isDustSuperUser: u.isDustSuperUser,
Expand Down
7 changes: 5 additions & 2 deletions front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions front/lib/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
Expand Down Expand Up @@ -65,7 +65,7 @@ User.init(
},
firstName: {
type: DataTypes.STRING,
allowNull: true,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
Expand Down
17 changes: 8 additions & 9 deletions front/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
};
17 changes: 8 additions & 9 deletions front/migrations/20231017_user_first_and_last_name.ts
Original file line number Diff line number Diff line change
@@ -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`);
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async function handler(
user: user,
context: {
username: user.username,
fullName: user.name,
fullName: user.fullName,
},
reaction: bodyValidation.right.reaction,
});
Expand All @@ -142,7 +142,7 @@ async function handler(
user: user,
context: {
username: user.username,
fullName: user.name,
fullName: user.fullName,
},
reaction: bodyValidation.right.reaction,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion front/pages/api/w/[wId]/assistant/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
4 changes: 3 additions & 1 deletion front/pages/api/w/[wId]/members/[userId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion front/pages/w/[wId]/assistant/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default function AssistantNew({
<div className="text-sm font-normal text-element-800">
<Page.Vertical gap="md" align="left">
<Page.Header
title={"Welcome " + user.name.split(" ")[0] + "!"} //Not solid
title={`Welcome ${user.firstName}!`}
icon={ChatBubbleLeftRightIcon}
/>
{/* FEATURED AGENTS */}
Expand Down
4 changes: 3 additions & 1 deletion front/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 12f7ac4

Please sign in to comment.