Skip to content

Commit

Permalink
Feat: Update end time when event host ended room (#197)
Browse files Browse the repository at this point in the history
* feat: update event end time
* remove: draft query
  • Loading branch information
ghaniswara authored Mar 28, 2024
1 parent fd7eb7d commit c4fbc45
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
83 changes: 83 additions & 0 deletions app/(server)/api/rooms/[roomID]/end/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { getCurrentAuthenticated } from '@/(server)/_shared/utils/get-current-authenticated';
import { eventRepo, roomRepo } from '@/(server)/api/_index';
import { cookies } from 'next/headers';
import { NextResponse } from 'next/server';
import * as Sentry from '@sentry/nextjs';

export async function POST({ params }: { params: { slugOrId: string } }) {
const slugOrId = params.slugOrId;
const cookieStore = cookies();
const requestToken = cookieStore.get('token');

try {
if (!requestToken) {
return NextResponse.json(
{
code: 401,
message: 'please check your authentication token',
},
{ status: 401 }
);
}

const response = await getCurrentAuthenticated(requestToken?.value);
const user = response.data ? response.data : null;

if (!user) {
return NextResponse.json(
{
code: 401,
message: 'unauthorized',
},
{ status: 401 }
);
}

const room = await roomRepo.getRoomById(slugOrId);

if (!room) {
return NextResponse.json(
{
code: 404,
message: 'room not found',
},
{ status: 404 }
);
}

if (room?.meta.type === 'meeting') {
const event = await eventRepo.getByRoomID(room.id);
if (event) {
event.endTime = new Date();
const newEvent = await eventRepo.updateEvent(user.id, event.id, event);

return NextResponse.json(
{
code: 200,
message: 'event ended',
data: newEvent,
},
{ status: 200 }
);
}
}

return NextResponse.json(
{
code: 200,
message: 'normal room ended',
data: room,
},
{ status: 200 }
);
} catch (e) {
Sentry.captureException(e);
return NextResponse.json(
{
code: 500,
message: 'internal server error',
},
{ status: 500 }
);
}
}
3 changes: 3 additions & 0 deletions app/(server)/api/rooms/[roomID]/register/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { roomService } from '@/(server)/api/_index';
import { NextResponse, type NextRequest } from 'next/server';
import * as Sentry from '@sentry/nextjs';

interface RegisterClientRequest {
uid?: string;
Expand Down Expand Up @@ -38,6 +39,8 @@ export async function POST(
{ status: 200 }
);
} catch (error) {
Sentry.captureException(error);

if (error instanceof Error) {
const response = {
code: 500,
Expand Down
8 changes: 6 additions & 2 deletions app/_features/room/components/button-leave.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useMetadataContext } from '@/_features/room/contexts/metadata-context';
import ArrowDownFillIcon from '@/_shared/components/icons/arrow-down-fill-icon';

export default function ButtonLeave() {
const { clientID } = useClientContext();
const { clientID, roomID } = useClientContext();
const { datachannels } = useDataChannelContext();
const { streams } = useParticipantContext();
const { isModerator, roomType } = useMetadataContext();
Expand Down Expand Up @@ -77,10 +77,14 @@ export default function ButtonLeave() {
},
})
);

if (roomType === 'event') {
navigator.sendBeacon(`/api/rooms/${roomID}/end`);
}
}
}
},
[clientID, datachannels, isModerator, roomType, streams]
[clientID, datachannels, isModerator, roomID, roomType, streams]
);

return (
Expand Down

0 comments on commit c4fbc45

Please sign in to comment.