Remember uploaded images
This commit is contained in:
@@ -9,6 +9,11 @@ async function getOwnHunts(userId: number | undefined) {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
picture: {
|
||||
select: {
|
||||
url: true
|
||||
}
|
||||
},
|
||||
createdAt: true,
|
||||
start: true,
|
||||
end: true,
|
||||
@@ -57,6 +62,11 @@ export default defineEventHandler(async (event) => {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
picture: {
|
||||
select: {
|
||||
url: true
|
||||
}
|
||||
},
|
||||
createdAt: true,
|
||||
start: true,
|
||||
end: true,
|
||||
|
||||
@@ -20,13 +20,21 @@ export default defineEventHandler(async (event) => {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
revealAnswers: true,
|
||||
revealQuests: true,
|
||||
picture: {
|
||||
select: {
|
||||
url: true
|
||||
}
|
||||
},
|
||||
description: true,
|
||||
createdAt: true,
|
||||
start: true,
|
||||
end: true,
|
||||
_count: {
|
||||
select: {
|
||||
teams: true
|
||||
teams: true,
|
||||
quests: true
|
||||
}
|
||||
},
|
||||
creator: {
|
||||
@@ -73,6 +81,11 @@ export default defineEventHandler(async (event) => {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
picture: {
|
||||
select: {
|
||||
url: true
|
||||
}
|
||||
},
|
||||
description: true,
|
||||
textRequired: true,
|
||||
pictureRequired: true,
|
||||
@@ -90,7 +103,11 @@ export default defineEventHandler(async (event) => {
|
||||
select: {
|
||||
id: true,
|
||||
text: true,
|
||||
picture: true,
|
||||
picture: {
|
||||
select: {
|
||||
url: true
|
||||
}
|
||||
},
|
||||
score: true,
|
||||
review: true,
|
||||
member: {
|
||||
@@ -159,7 +176,7 @@ export default defineEventHandler(async (event) => {
|
||||
ownTeam,
|
||||
loggedIn: !!user.user,
|
||||
totalScore: ownTeam !== null ? totalScore : undefined,
|
||||
quests: mappedQuests,
|
||||
quests: hunt.revealQuests ? mappedQuests : null,
|
||||
...hunt
|
||||
};
|
||||
});
|
||||
|
||||
@@ -20,6 +20,11 @@ export default defineEventHandler(async (event) => {
|
||||
id: true,
|
||||
title: true,
|
||||
description: true,
|
||||
picture: {
|
||||
select: {
|
||||
url: true
|
||||
}
|
||||
},
|
||||
question: true,
|
||||
optional: true,
|
||||
pictureRequired: true,
|
||||
@@ -32,14 +37,19 @@ export default defineEventHandler(async (event) => {
|
||||
hunt: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true
|
||||
name: true,
|
||||
revealQuests: true
|
||||
}
|
||||
},
|
||||
answers: {
|
||||
select: {
|
||||
id: true,
|
||||
text: true,
|
||||
picture: true,
|
||||
picture: {
|
||||
select: {
|
||||
url: true
|
||||
}
|
||||
},
|
||||
score: true,
|
||||
review: true,
|
||||
member: {
|
||||
@@ -79,6 +89,10 @@ export default defineEventHandler(async (event) => {
|
||||
throw createError({ status: 404, statusText: 'Not found!' });
|
||||
}
|
||||
|
||||
if (!_quest.hunt.revealQuests) {
|
||||
throw createError({ status: 404, statusText: 'Not found!' });
|
||||
}
|
||||
|
||||
const { answers, ...quest } = _quest;
|
||||
|
||||
const answer = answers.length > 0 ? answers[0] : null;
|
||||
|
||||
@@ -115,9 +115,9 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
// TODO: prevent updating reviewed answers
|
||||
// TODO: respect end of hunt
|
||||
// TODO: keep old images
|
||||
|
||||
let picture: string | undefined = undefined;
|
||||
let picture: { creatorId: number; url: string; uuid: string } | undefined =
|
||||
undefined;
|
||||
|
||||
if (data.image) {
|
||||
const uuid = randomUUID();
|
||||
@@ -131,30 +131,75 @@ export default defineEventHandler(async (event) => {
|
||||
'Content-Type': data.image.type
|
||||
}
|
||||
);
|
||||
picture = `https://${s3Bucket}.${s3Host}/${uuid}`;
|
||||
picture = {
|
||||
creatorId: userId,
|
||||
url: `https://${s3Bucket}.${s3Host}/${uuid}`,
|
||||
uuid
|
||||
};
|
||||
}
|
||||
|
||||
if (prevAnswer.id) {
|
||||
await prisma.questAnswer.update({
|
||||
where: {
|
||||
id: prevAnswer.id
|
||||
},
|
||||
data: {
|
||||
memberId: userId,
|
||||
text: data.answer,
|
||||
picture
|
||||
if (picture) {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
if (prevAnswer.id) {
|
||||
const file = await tx.file.create({
|
||||
data: {
|
||||
...picture,
|
||||
answerId: prevAnswer.id
|
||||
},
|
||||
select: { id: true }
|
||||
});
|
||||
await tx.questAnswer.update({
|
||||
where: {
|
||||
id: prevAnswer.id
|
||||
},
|
||||
data: {
|
||||
memberId: userId,
|
||||
text: data.answer,
|
||||
pictureId: file.id
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const answer = await tx.questAnswer.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
memberId: userId,
|
||||
questId: quest.id,
|
||||
text: data.answer
|
||||
},
|
||||
select: { id: true }
|
||||
});
|
||||
await tx.file.create({
|
||||
data: {
|
||||
...picture,
|
||||
answerId: answer.id,
|
||||
answers: {
|
||||
connect: answer
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
await prisma.questAnswer.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
memberId: userId,
|
||||
questId: quest.id,
|
||||
text: data.answer,
|
||||
picture
|
||||
}
|
||||
});
|
||||
if (prevAnswer.id) {
|
||||
await prisma.questAnswer.update({
|
||||
where: {
|
||||
id: prevAnswer.id
|
||||
},
|
||||
data: {
|
||||
memberId: userId,
|
||||
text: data.answer
|
||||
}
|
||||
});
|
||||
} else {
|
||||
await prisma.questAnswer.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
memberId: userId,
|
||||
questId: quest.id,
|
||||
text: data.answer
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
|
||||
Reference in New Issue
Block a user