task agent validation: if agent is not in the crew, replace task agent with null

This commit is contained in:
Eng. Elias
2024-02-29 17:56:42 +04:00
parent fc7d171b8b
commit 3344c01c7b
3 changed files with 23 additions and 9 deletions

View File

@@ -62,9 +62,7 @@ const resolvers = {
for (let task of body.tasks) { for (let task of body.tasks) {
let agent = null; let agent = null;
if (task.agent) { if (task.agent) {
agent = await prisma.agent.findFirst({ agent = crew.find((a) => a.id === task.agent) ?? null;
where: { id: task.agent },
});
} }
tasks.push({ tasks.push({
...task, ...task,
@@ -97,9 +95,7 @@ const resolvers = {
for (let task of body.tasks) { for (let task of body.tasks) {
let agent = null; let agent = null;
if (task.agent) { if (task.agent) {
agent = await prisma.agent.findFirst({ agent = crew.find((a) => a.id === task.agent) ?? null;
where: { id: task.agent },
});
} }
tasks.push({ tasks.push({
...task, ...task,

View File

@@ -127,14 +127,23 @@ export default function MissionModal(props: {
value: agent.id, value: agent.id,
}))} }))}
multiple multiple
value={tempMission.crew.map((agent) => agent.id!)}
onValueChange={(event: any) => { onValueChange={(event: any) => {
const newValue = event.map((item: any) => item.value); const newValue = event.map((item: any) => item.value);
const newCrew = agentsData.agents.filter( const newCrew = agentsData.agents.filter(
(agent: Agent) => newValue.includes(agent.id) (agent: Agent) => newValue.includes(agent.id)
); );
const newTasks = tempMission.tasks.map((task) => ({
...task,
agent:
newCrew.find(
(agent: Agent) => agent.id === task.agent?.id
) ?? null,
}));
setTempMission((prevState) => ({ setTempMission((prevState) => ({
...prevState!, ...prevState!,
crew: newCrew, crew: newCrew,
tasks: newTasks,
})); }));
}} }}
theme={selectTheme} theme={selectTheme}
@@ -208,8 +217,8 @@ export default function MissionModal(props: {
{isEdit ? ( {isEdit ? (
<div> <div>
<MissionTaskEditor <MissionTaskEditor
mission={mission!} mission={tempMission!}
agents={mission?.crew!} agents={tempMission?.crew!}
onMissionChange={(mission: Mission) => { onMissionChange={(mission: Mission) => {
setTempMission(mission); setTempMission(mission);
}} }}
@@ -267,7 +276,7 @@ export default function MissionModal(props: {
<TERipple rippleColor="light"> <TERipple rippleColor="light">
<Button <Button
color="white" color="white"
onClick={() => setShowModal(false)} onClick={() => setEdit(false)}
placeholder={undefined} placeholder={undefined}
> >
Cancel Cancel
@@ -282,6 +291,7 @@ export default function MissionModal(props: {
handleUpdateMission(tempMission) handleUpdateMission(tempMission)
.then(() => { .then(() => {
setShowModal(false); setShowModal(false);
setEdit(false);
ReactSwal.fire({ ReactSwal.fire({
title: "Update Mission", title: "Update Mission",
text: "Mission updated successfully", text: "Mission updated successfully",

View File

@@ -119,9 +119,17 @@ function NewMissionModal(props: {
const newCrew = agentsData.agents.filter( const newCrew = agentsData.agents.filter(
(agent: Agent) => newValue.includes(agent.id) (agent: Agent) => newValue.includes(agent.id)
); );
const newTasks = tempMission.tasks.map((task) => ({
...task,
agent:
newCrew.find(
(agent: Agent) => agent.id === task.agent?.id
) ?? null,
}));
setTempMission((prevState) => ({ setTempMission((prevState) => ({
...prevState!, ...prevState!,
crew: newCrew, crew: newCrew,
tasks: newTasks,
})); }));
}} }}
theme={selectTheme} theme={selectTheme}