Fix Activities and Tasks modules (#2561)
* Fix activities * Fix Timeline * Refactor useCreateOne and useUpdateOne records * Fix seeds
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import { Activity, ActivityType } from '@/activities/types/Activity';
|
||||
import { currentUserState } from '@/auth/states/currentUserState';
|
||||
import { ActivityTarget } from '@/activities/types/ActivityTarget';
|
||||
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
|
||||
import { useCreateOneObjectRecord } from '@/object-record/hooks/useCreateOneObjectRecord';
|
||||
import { useRightDrawer } from '@/ui/layout/right-drawer/hooks/useRightDrawer';
|
||||
@@ -13,14 +12,18 @@ import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope
|
||||
import { activityTargetableEntityArrayState } from '../states/activityTargetableEntityArrayState';
|
||||
import { viewableActivityIdState } from '../states/viewableActivityIdState';
|
||||
import { ActivityTargetableEntity } from '../types/ActivityTargetableEntity';
|
||||
import { getRelationData } from '../utils/getRelationData';
|
||||
import { getTargetableEntitiesWithParents } from '../utils/getTargetableEntitiesWithParents';
|
||||
|
||||
export const useOpenCreateActivityDrawer = () => {
|
||||
const { openRightDrawer } = useRightDrawer();
|
||||
const { createOneObject } = useCreateOneObjectRecord({
|
||||
objectNamePlural: 'activitiesV2',
|
||||
});
|
||||
const currentUser = useRecoilValue(currentUserState);
|
||||
const { createOneObject: createOneActivityTarget } =
|
||||
useCreateOneObjectRecord<ActivityTarget>({
|
||||
objectNameSingular: 'activityTargetV2',
|
||||
});
|
||||
const { createOneObject: createOneActivity } =
|
||||
useCreateOneObjectRecord<Activity>({
|
||||
objectNameSingular: 'activityV2',
|
||||
});
|
||||
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
|
||||
const setHotkeyScope = useSetHotkeyScope();
|
||||
|
||||
@@ -29,7 +32,7 @@ export const useOpenCreateActivityDrawer = () => {
|
||||
);
|
||||
const [, setViewableActivityId] = useRecoilState(viewableActivityIdState);
|
||||
|
||||
return ({
|
||||
return async ({
|
||||
type,
|
||||
targetableEntities,
|
||||
assigneeId,
|
||||
@@ -38,33 +41,35 @@ export const useOpenCreateActivityDrawer = () => {
|
||||
targetableEntities?: ActivityTargetableEntity[];
|
||||
assigneeId?: string;
|
||||
}) => {
|
||||
const now = new Date().toISOString();
|
||||
const targetableEntitiesWithRelations = targetableEntities
|
||||
? getTargetableEntitiesWithParents(targetableEntities)
|
||||
: [];
|
||||
|
||||
createOneObject?.({
|
||||
id: v4(),
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
author: { connect: { id: currentUser?.id ?? '' } },
|
||||
workspaceMemberAuthor: {
|
||||
connect: { id: currentWorkspaceMember?.id ?? '' },
|
||||
},
|
||||
assignee: { connect: { id: assigneeId ?? currentUser?.id ?? '' } },
|
||||
workspaceMemberAssignee: {
|
||||
connect: { id: currentWorkspaceMember?.id ?? '' },
|
||||
},
|
||||
const createdActivity = await createOneActivity?.({
|
||||
authorId: currentWorkspaceMember?.id,
|
||||
assigneeId: assigneeId ?? currentWorkspaceMember?.id,
|
||||
type: type,
|
||||
activityTargets: {
|
||||
createMany: {
|
||||
data: targetableEntities ? getRelationData(targetableEntities) : [],
|
||||
skipDuplicates: true,
|
||||
},
|
||||
},
|
||||
onCompleted: (data: Activity) => {
|
||||
setHotkeyScope(RightDrawerHotkeyScope.RightDrawer, { goto: false });
|
||||
setViewableActivityId(data.id);
|
||||
setActivityTargetableEntityArray(targetableEntities ?? []);
|
||||
openRightDrawer(RightDrawerPages.CreateActivity);
|
||||
},
|
||||
});
|
||||
|
||||
if (!createdActivity) {
|
||||
return;
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
targetableEntitiesWithRelations.map(async (targetableEntity) => {
|
||||
await createOneActivityTarget?.({
|
||||
companyId:
|
||||
targetableEntity.type === 'Company' ? targetableEntity.id : null,
|
||||
personId:
|
||||
targetableEntity.type === 'Person' ? targetableEntity.id : null,
|
||||
activityId: createdActivity.id,
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
setHotkeyScope(RightDrawerHotkeyScope.RightDrawer, { goto: false });
|
||||
setViewableActivityId(createdActivity.id);
|
||||
setActivityTargetableEntityArray(targetableEntities ?? []);
|
||||
openRightDrawer(RightDrawerPages.CreateActivity);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user