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 Nov 3, 2023
1 parent cfae126 commit 4cfbc25
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 44 deletions.
4 changes: 3 additions & 1 deletion front/lib/api/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion front/lib/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export async function getMembers(auth: Authenticator): Promise<UserType[]> {
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,
Expand Down
10 changes: 8 additions & 2 deletions front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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);
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 @@ -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,
},
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 @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion front/pages/poke/[wId]/memberships.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const MembershipsPage = ({
>
<div className="flex items-center justify-between">
<h3 className="mb-2 text-lg font-semibold">
{m.username} ({m.name})
{m.username} ({m.fullName})
</h3>
</div>
<p>
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 @@ -183,7 +183,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
24 changes: 15 additions & 9 deletions front/pages/w/[wId]/members/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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())
),
Expand Down Expand Up @@ -275,13 +275,17 @@ export default function WorkspaceAdmin({
{isInvitation(item) ? (
<Avatar size="sm" />
) : (
<Avatar visual={item.image} name={item.name} size="sm" />
<Avatar
visual={item.image}
name={item.fullName}
size="sm"
/>
)}
</div>
<div className="flex grow flex-col gap-1 sm:flex-row sm:gap-3">
{!isInvitation(item) && (
<div className="font-medium text-element-900">
{item.name}
{item.fullName}
{user?.id === item.id && " (you)"}
</div>
)}
Expand Down Expand Up @@ -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`);
}
Expand All @@ -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);
Expand All @@ -687,9 +691,11 @@ function ChangeMemberModal({
>
<div className="mt-6 flex flex-col gap-9 px-2 text-sm text-element-700">
<div className="flex items-center gap-4">
<Avatar size="lg" visual={member.image} name={member.name} />
<Avatar size="lg" visual={member.image} name={member.fullName} />
<div className="flex grow flex-col">
<div className="font-semibold text-element-900">{member.name}</div>
<div className="font-semibold text-element-900">
{member.fullName}
</div>
<div className="font-normal">{member.email}</div>
</div>
</div>
Expand Down Expand Up @@ -747,7 +753,7 @@ function ChangeMemberModal({
<div className="mt-6 flex flex-col gap-6 px-2">
<div>
Revoke access for user{" "}
<span className="font-bold">{member.name}</span>?
<span className="font-bold">{member.fullName}</span>?
</div>
<div className="flex gap-2">
<Button
Expand Down
4 changes: 2 additions & 2 deletions front/pages/w/[wId]/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export default function Welcome({
gaTrackingId,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
const router = useRouter();
const [firstName, setFirstName] = useState<string>(user.name.split(" ")[0]);
const [lastName, setLastName] = useState<string>(user.name.split(" ")[1]);
const [firstName, setFirstName] = useState<string>(user.firstName);
const [lastName, setLastName] = useState<string>(user.lastName || "");
const [expertise, setExpertise] = useState<string>(defaultExpertise);
const [adminInterest, setAdminInterest] =
useState<string>(defaultAdminInterest);
Expand Down
4 changes: 3 additions & 1 deletion front/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 4cfbc25

Please sign in to comment.