Implement session invites

This commit is contained in:
Pijus Kamandulis
2024-10-12 00:04:36 +03:00
parent 9b45d372fa
commit 1463c05731
16 changed files with 795 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
declare type AppwriteRequest = {
bodyRaw: string;
body: Object;
headers: Object;
scheme: string;
method: string;
url: string;
host: string;
port: number;
path: string;
queryString: string;
query: Object;
};
declare type AppwriteSendReturn = {
body: any;
statusCode: number;
headers: Object;
};
declare type AppwriteResponse = {
empty: () => AppwriteSendReturn;
json: (obj: any, statusCode?: number, headers?: Object) => AppwriteSendReturn;
text: (text: string) => AppwriteSendReturn;
redirect: (
url: string,
statusCode?: number,
headers?: Object,
) => AppwriteSendReturn;
send: (
body: any,
statusCode?: number,
headers?: Object,
) => AppwriteSendReturn;
};
declare type AppwriteRuntimeContext = {
req: AppwriteRequest;
res: AppwriteResponse;
log: (message: any) => void;
error: (message: any) => void;
};
export {
AppwriteRequest,
AppwriteSendReturn,
AppwriteResponse,
AppwriteRuntimeContext,
};

View File

@@ -0,0 +1,148 @@
import { Client, Databases } from 'node-appwrite';
import { AppwriteRuntimeContext, AppwriteSendReturn } from './definitions.mjs';
const joinSession = async ({
client,
estimationId,
userId,
ctx: { error, res },
}: {
client: Client;
estimationId: string;
userId: string;
ctx: AppwriteRuntimeContext;
}): Promise<AppwriteSendReturn> => {
const databases = new Databases(client);
let estimation;
try {
estimation = await databases.getDocument(
Bun.env.APPWRITE_DATABASE_ID,
Bun.env.APPWRITE_ESTIMATION_SESSION_COLLECTION_ID,
estimationId,
);
} catch (e) {
error({ e });
return res.json(
{
message: 'Estimation with this id does not exist',
},
400,
);
}
try {
const permissions: string[] = estimation['$permissions'];
permissions.push(`read("user:${userId}")`);
permissions.push(`update("user:${userId}")`);
await databases.updateDocument(
Bun.env.APPWRITE_DATABASE_ID,
Bun.env.APPWRITE_ESTIMATION_SESSION_COLLECTION_ID,
estimationId,
{},
permissions,
);
return res.json({
message: 'Estimation session joined',
});
} catch (e) {
error({ e });
return res.json(
{
message: 'Failed to join estimation session',
},
500,
);
}
};
const getSessionInfo = async ({
client,
estimationId,
ctx: { log, res },
}: {
client: Client;
estimationId: string;
ctx: AppwriteRuntimeContext;
}): Promise<AppwriteSendReturn> => {
const databases = new Databases(client);
try {
const estimation = await databases.getDocument(
Bun.env.APPWRITE_DATABASE_ID,
Bun.env.APPWRITE_ESTIMATION_SESSION_COLLECTION_ID,
estimationId,
);
return res.json({
result: {
id: estimation.$id,
name: estimation.name,
},
});
} catch (e) {
console.log({ e });
log({ e });
return res.json(
{
message: 'Estimation with this id does not exist',
},
400,
);
}
};
export default async (ctx: AppwriteRuntimeContext) => {
const { req, res } = ctx;
const userId = req.headers['x-appwrite-user-id'];
if (!userId) {
return res.json(
{
message: 'Unauthorized',
},
401,
);
}
const action = req.query['action'];
const estimationId = req.query['estimationId'];
if (!action || !estimationId) {
return res.json(
{
message: 'Bad request',
},
400,
);
}
const client = new Client()
.setEndpoint(Bun.env.APPWRITE_FUNCTION_API_ENDPOINT)
.setProject(Bun.env.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.headers['x-appwrite-key'] ?? '');
if (action === 'get-info') {
return await getSessionInfo({
client,
ctx,
estimationId,
});
}
if (action === 'join') {
return await joinSession({
client,
estimationId,
userId,
ctx,
});
}
return res.json(
{
message: 'Not found',
},
404,
);
};