Skip to content

Commit

Permalink
feat(UserProfile): add curator in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Katrin-kudryash committed Nov 18, 2024
1 parent c1f0790 commit a6f4ac5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "_userCurators" DROP CONSTRAINT "_userCurators_B_fkey";

-- AddForeignKey
ALTER TABLE "_userCurators" ADD CONSTRAINT "_userCurators_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
35 changes: 18 additions & 17 deletions src/components/UserUpdateForm/UserUpdateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Text,
nullable,
} from '@taskany/bricks';
import { Checkbox, FormControl } from '@taskany/bricks/harmony';
import { Checkbox } from '@taskany/bricks/harmony';
import { zodResolver } from '@hookform/resolvers/zod';
import { User } from 'prisma/prisma-client';
import { gray8 } from '@taskany/colors';
Expand All @@ -25,7 +25,7 @@ import { OrganizationUnitComboBox } from '../OrganizationUnitComboBox/Organizati
import { AddSupplementalPosition } from '../AddSupplementalPosition/AddSupplementalPosition';
import { SupplementalPositionItem } from '../SupplementalPositionItem/SupplementalPositionItem';
import { useSupplementalPositionMutations } from '../../modules/supplementalPositionHooks';
import { UserSelect } from '../UserSelect/UserSelect';
// import { UserSelect } from '../UserSelect/UserSelect';

import { tr } from './UserUpdateForm.i18n';
import s from './UserUpdateForm.module.css';
Expand All @@ -34,9 +34,9 @@ interface UserDataFormProps {
user: User & UserSupervisor & UserOrganizationUnit & UserSupplementalPositions & UserCurators;
onClose: () => void;
}
interface UserFormExternalTeamBlockType {
curatorIds?: string[];
}
// interface UserFormExternalTeamBlockType {
// curatorIds?: string[];
// }
/* TODO: Add coordinator field https://github.com/taskany-inc/crew/issues/278*/
export const UserUpdateForm = ({ onClose, user }: UserDataFormProps) => {
const { editUser } = useUserMutations();
Expand All @@ -52,7 +52,7 @@ export const UserUpdateForm = ({ onClose, user }: UserDataFormProps) => {
handleSubmit,
setValue,
watch,
trigger,
// trigger,
formState: { isSubmitted, errors },
} = useForm<EditUserFields>({
defaultValues: {
Expand All @@ -61,7 +61,7 @@ export const UserUpdateForm = ({ onClose, user }: UserDataFormProps) => {
name: user.name || undefined,
savePreviousName: undefined,
organizationUnitId: user.organizationUnitId || undefined,
curatorIds: user.curators?.map(({ id }) => id),
// curatorIds: user.curators?.map(({ id }) => id),
},
mode: 'onChange',
resolver: zodResolver(editUserFieldsSchema),
Expand All @@ -74,13 +74,14 @@ export const UserUpdateForm = ({ onClose, user }: UserDataFormProps) => {
onClose();
};

const onUsersChange = (users: User[], type: keyof UserFormExternalTeamBlockType) => {
setValue(
type,
users.map((user) => user.id),
);
trigger(type);
};
// const onUsersChange = (users: User[], type: keyof UserFormExternalTeamBlockType) => {
// setValue(
// type,
// users.map((user) => user.id),
// );
// trigger(type);
// };

return (
<>
<ModalHeader>
Expand Down Expand Up @@ -119,14 +120,14 @@ export const UserUpdateForm = ({ onClose, user }: UserDataFormProps) => {
/>
</div>
<div className={s.Field}>
<FormControl label={tr('Curator')} required>
{/* <FormControl required>
<UserSelect
mode="multiple"
// selectedUsers={watch('curatorIds') ? watch('curatorIds') : undefined}
selectedUsers={watch('curatorIds') ? watch('curatorIds') : undefined}
onChange={(users) => onUsersChange(users, 'curatorIds')}
error={errors.curatorIds}
/>
</FormControl>
</FormControl> */}
</div>
<div className={s.Field}>
<Text className={s.Text} weight="bold" color={gray8}>
Expand Down
2 changes: 1 addition & 1 deletion src/modules/externalUserTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface ExternalUserUpdate {
email: string;
name?: string;
supervisorId?: string | null;
curatorIds?: string[] | null;
curators?: string[] | null;
active?: boolean;
login?: string;
}
30 changes: 23 additions & 7 deletions src/modules/userMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,40 @@ export const userMethods = {
return prisma.user.findMany({ where: { memberships: { some: { groupId } }, active: { not: true } } });
},

edit: async ({ id, savePreviousName, supplementalPosition, ...data }: EditUserFields) => {
if (data.organizationUnitId) {
edit: async ({
id,
savePreviousName,
supplementalPosition,
organizationUnitId,
curatorIds,
...data
}: EditUserFields) => {
const updateUser: Prisma.UserUpdateInput = data;
// const user = await prisma.user.findUnique({
// where: { id },
// include: { curators: true },
// });

if (organizationUnitId) {
const newOrganization = await prisma.organizationUnit.findUnique({
where: {
id: data.organizationUnitId,
id: organizationUnitId,
},
});

if (!newOrganization) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: `No organization with id ${data.organizationUnitId}`,
message: `No organization with id ${organizationUnitId}`,
});
}

updateUser.organizationUnit = { connect: { id: organizationUnitId } };
}

// if (user?.curators) {
// updateUser.curators = { connect: { curators: user?.curators } };
// }
if (supplementalPosition) {
const supplementalOrganization = await prisma.organizationUnit.findUnique({
where: {
Expand All @@ -441,7 +459,6 @@ export const userMethods = {
await externalUserMethods.update(id, {
name: data.name,
supervisorId: data.supervisorId,
curatorIds: data.curatorIds,
});

if (savePreviousName) {
Expand All @@ -450,8 +467,7 @@ export const userMethods = {
await prisma.userNames.create({ data: { userId: id, name: userBeforeUpdate.name } });
}
}

return prisma.user.update({ where: { id }, data });
return prisma.user.update({ where: { id }, data: updateUser });
},

editActiveState: async (data: EditUserActiveState): Promise<User> => {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/userSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const editUserSchema = z.object({
name: z.string().optional(),
savePreviousName: z.boolean().optional(),
supervisorId: z.string().nullish(),
curatorIds: z.array(z.string()).nullish(),
curatorIds: z.string().nullish(),
supplementalPosition: z
.object({
organizationUnitId: z.string(),
Expand Down
7 changes: 4 additions & 3 deletions src/modules/userTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ export interface UserMemberships {
export interface UserSupervisor {
supervisor: Nullish<User>;
}
export interface UserCurators {
curators: Nullish<User[]>;
}

export interface UserSupervisorOf {
supervisorOf: Nullish<User[]>;
}

export interface UserCurators {
curators: Nullish<User[]>;
}
export interface UserCuratorOf {
curatorOf: Nullish<User[]>;
}
Expand Down
1 change: 0 additions & 1 deletion src/trpc/router/userRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export const userRouter = router({
organizationalUnitId: userBefore.organizationUnitId ?? undefined,
email: userBefore.email,
savePreviousName: input.savePreviousName,
// curators: userBefore.curators
},
{
name: result.name,
Expand Down

0 comments on commit a6f4ac5

Please sign in to comment.