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

View File

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

View File

@@ -119,9 +119,17 @@ function NewMissionModal(props: {
const newCrew = agentsData.agents.filter(
(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) => ({
...prevState!,
crew: newCrew,
tasks: newTasks,
}));
}}
theme={selectTheme}