Skip to content

Commit

Permalink
Merge pull request #50 from cabcookie:staging
Browse files Browse the repository at this point in the history
Kontexte von Meetings und Projekten ändern und Projekten CRM Projekte verlinken
  • Loading branch information
cabcookie authored Apr 30, 2024
2 parents 311fe00 + 7831859 commit 498d9f1
Show file tree
Hide file tree
Showing 28 changed files with 834 additions and 86 deletions.
26 changes: 26 additions & 0 deletions amplify/data/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,31 @@ const schema = a.schema({
createdOn: a.datetime(),
})
.authorization((allow) => [allow.owner()]),
CrmProjectProjects: a
.model({
owner: a
.string()
.authorization((allow) => [allow.owner().to(["read", "delete"])]),
projectId: a.id().required(),
crmProjectId: a.id().required(),
project: a.belongsTo("Projects", "projectId"),
crmProject: a.belongsTo("CrmProject", "crmProjectId"),
})
.authorization((allow) => [allow.owner()]),
CrmProject: a
.model({
owner: a
.string()
.authorization((allow) => [allow.owner().to(["read", "delete"])]),
name: a.string().required(),
crmId: a.string(),
annualRecurringRevenue: a.integer(),
totalContractVolume: a.integer(),
closeDate: a.date().required(),
projects: a.hasMany("CrmProjectProjects", "crmProjectId"),
stage: a.string().required(),
})
.authorization((allow) => [allow.owner()]),
Projects: a
.model({
owner: a
Expand All @@ -231,6 +256,7 @@ const schema = a.schema({
activities: a.hasMany("ProjectActivity", "projectsId"),
dayTasks: a.hasMany("DayProjectTask", "projectsDayTasksId"),
todos: a.hasMany("DayPlanTodo", "projectsTodosId"),
crmProjects: a.hasMany("CrmProjectProjects", "projectId"),
})
.authorization((allow) => [allow.owner()]),
});
Expand Down
49 changes: 38 additions & 11 deletions api/ContextProjects.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC, ReactNode, createContext, useContext } from "react";
import { type Schema } from "@/amplify/data/resource";
import { SelectionSet, generateClient } from "aws-amplify/data";
import useSWR from "swr";
import useSWR, { KeyedMutator } from "swr";
import { Context } from "@/contexts/ContextContext";
import { handleApiErrors } from "./globals";
import { addDaysToDate } from "@/helpers/functional";
Expand Down Expand Up @@ -42,6 +42,11 @@ interface ProjectsContextType {
projectId: string,
accountId: string
) => Promise<string | undefined>;
updateProjectContext: (
projectId: string,
context: Context
) => Promise<string | undefined>;
mutateProjects: KeyedMutator<Project[] | undefined>;
}

export type Project = {
Expand All @@ -56,6 +61,7 @@ export type Project = {
context: Context;
accountIds: string[];
activityIds: string[];
crmProjectIds: string[];
};

const selectionSet = [
Expand All @@ -73,6 +79,7 @@ const selectionSet = [
"activities.activity.id",
"activities.activity.finishedOn",
"activities.activity.createdAt",
"crmProjects.crmProject.id",
] as const;

type ProjectData = SelectionSet<Schema["Projects"], typeof selectionSet>;
Expand All @@ -89,6 +96,7 @@ export const mapProject: (project: ProjectData) => Project = ({
context,
accounts,
activities,
crmProjects,
}) => ({
id,
project,
Expand All @@ -114,6 +122,7 @@ export const mapProject: (project: ProjectData) => Project = ({
}))
.sort((a, b) => b.finishedOn.getTime() - a.finishedOn.getTime())
.map(({ id }) => id),
crmProjectIds: crmProjects.map(({ crmProject: { id } }) => id),
});

const fetchProjects = (context?: Context) => async () => {
Expand All @@ -130,7 +139,7 @@ const fetchProjects = (context?: Context) => async () => {
},
],
},
limit: 500,
limit: 5000,
selectionSet,
});
if (errors) throw errors;
Expand All @@ -150,7 +159,7 @@ export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
data: projects,
error: errorProjects,
isLoading: loadingProjects,
mutate,
mutate: mutateProjects,
} = useSWR(`/api/projects/${context}`, fetchProjects(context));

const createProject = async (
Expand All @@ -168,18 +177,19 @@ export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
context,
accountIds: [],
activityIds: [],
crmProjectIds: [],
};

const updatedProjects = [...(projects || []), newProject];
mutate(updatedProjects, false);
mutateProjects(updatedProjects, false);

const { data, errors } = await client.models.Projects.create({
context,
project: projectName,
done: false,
});
if (errors) handleApiErrors(errors, "Error creating project");
mutate(updatedProjects);
mutateProjects(updatedProjects);
return data;
};

Expand All @@ -199,13 +209,13 @@ export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
? project
: { ...project, activityIds: [activity.id, ...project.activityIds] }
) || [];
mutate(updated, false);
mutateProjects(updated, false);
const { data, errors } = await client.models.ProjectActivity.create({
activityId: activity.id,
projectsId: projectId,
});
if (errors) handleApiErrors(errors, "Error linking activity with project");
mutate(updated);
mutateProjects(updated);
return data.activityId;
};

Expand Down Expand Up @@ -247,7 +257,7 @@ export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
: othersNextActions,
}
) || [];
mutate(updated, false);
mutateProjects(updated, false);
const newProject = {
id,
project,
Expand All @@ -267,7 +277,7 @@ export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
};
const { data, errors } = await client.models.Projects.update(newProject);
if (errors) handleApiErrors(errors, "Error updating project");
mutate(updated);
mutateProjects(updated);
return data?.id;
};

Expand Down Expand Up @@ -302,16 +312,31 @@ export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
? p
: { ...p, accountIds: [...p.accountIds, accountId] }
) || [];
mutate(updated, false);
mutateProjects(updated, false);
const { data, errors } = await client.models.AccountProjects.create({
projectsId: projectId,
accountId,
});
if (errors) handleApiErrors(errors, "Error adding account to project");
mutate(updated);
mutateProjects(updated);
return data?.id;
};

const updateProjectContext = async (
projectId: string,
newContext: Context
) => {
const { data, errors } = await client.models.Projects.update({
id: projectId,
context: newContext,
});
if (errors) {
handleApiErrors(errors, "Error updating project's context");
return;
}
return data.id;
};

return (
<ProjectsContext.Provider
value={{
Expand All @@ -326,6 +351,8 @@ export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
saveProjectDates,
updateProjectState,
addAccountToProject,
updateProjectContext,
mutateProjects,
}}
>
{children}
Expand Down
71 changes: 71 additions & 0 deletions api/useCrmProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { type Schema } from "@/amplify/data/resource";
import { generateClient } from "aws-amplify/data";
import useSWR from "swr";
import {
CrmProject,
mapCrmProject,
selectionSetCrmProject,
} from "./useCrmProjects";
import { getDayOfDate } from "@/helpers/functional";
import { handleApiErrors } from "./globals";
import { Project, useProjectsContext } from "./ContextProjects";
const client = generateClient<Schema>();

const fetchCrmProject = (projectId?: string) => async () => {
if (!projectId) return;
const { data, errors } = await client.models.CrmProject.get(
{ id: projectId },
{ selectionSet: selectionSetCrmProject }
);
if (errors) throw errors;
return mapCrmProject(data);
};

const useCrmProject = (projectId?: string) => {
const {
data: crmProject,
error: errorCrmProject,
isLoading: loadingCrmProject,
mutate,
} = useSWR(`/api/crm-projects/${projectId}`, fetchCrmProject(projectId));
const { projects, mutateProjects } = useProjectsContext();

const createCrmProject = async (project: CrmProject) => {
const { data: newProject, errors: projectErrors } =
await client.models.CrmProject.create({
closeDate: getDayOfDate(project.closeDate),
name: project.name,
stage: project.stage,
annualRecurringRevenue: project.arr,
crmId: project.crmId,
totalContractVolume: project.tcv,
});
if (projectErrors) {
handleApiErrors(projectErrors, "Error creating CRM project");
return;
}
if (!newProject) return;
const { data, errors } = await client.models.CrmProjectProjects.create({
projectId: project.projectIds[0],
crmProjectId: newProject.id,
});
if (errors) {
handleApiErrors(errors, "Error linking CRM project to project");
return;
}
if (projects) {
mutateProjects(
projects.map((p) =>
p.id !== project.projectIds[0]
? p
: { ...p, crmProjectIds: [...p.crmProjectIds, newProject.id] }
)
);
}
return newProject.id;
};

return { crmProject, errorCrmProject, loadingCrmProject, createCrmProject };
};

export default useCrmProject;
Loading

0 comments on commit 498d9f1

Please sign in to comment.