feat(api): support user participant invitation when creating events
This commit is contained in:
parent
ce0223d56e
commit
60ff0b54e1
1 changed files with 29 additions and 2 deletions
|
@ -10,10 +10,24 @@ const postEventSchema = z
|
|||
start_time: z.iso.datetime(),
|
||||
end_time: z.iso.datetime(),
|
||||
location: z.string().optional().default(''),
|
||||
participants: z.array(z.string()).optional(),
|
||||
})
|
||||
.refine((data) => new Date(data.start_time) < new Date(data.end_time), {
|
||||
error: 'Start time must be before end time',
|
||||
});
|
||||
})
|
||||
.refine(
|
||||
async (data) =>
|
||||
!data.participants ||
|
||||
(await Promise.all(
|
||||
data.participants.map(async (userId) => {
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
return !!user;
|
||||
}),
|
||||
).then((results) => results.every(Boolean))),
|
||||
{
|
||||
error: 'One or more participant user IDs are invalid',
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
|
@ -154,6 +168,11 @@ export const GET = auth(async (req) => {
|
|||
* format: date-time
|
||||
* location:
|
||||
* type: string
|
||||
* participants:
|
||||
* type: array
|
||||
* items:
|
||||
* type: string
|
||||
* description: User ID of a participant
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Event created successfully.
|
||||
|
@ -215,7 +234,8 @@ export const POST = auth(async (req) => {
|
|||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const { title, description, start_time, end_time, location } = data.data;
|
||||
const { title, description, start_time, end_time, location, participants } =
|
||||
data.data;
|
||||
|
||||
const newEvent = await prisma.meeting.create({
|
||||
data: {
|
||||
|
@ -225,6 +245,13 @@ export const POST = auth(async (req) => {
|
|||
end_time,
|
||||
location,
|
||||
organizer_id: req.auth.user.id,
|
||||
participants: participants
|
||||
? {
|
||||
create: participants.map((userId) => ({
|
||||
user: { connect: { id: userId } },
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue