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