Skip to content

Commit 38bf1f7

Browse files
author
Gillian
authored
Add endpoint to add Clack channel
Will be used in upcoming diffs (as demoed on Thursday) to add channel from UI Test Plan: Used in upcoming diffs Reviewers: jwatzman Reviewed By: jwatzman Pull Request: #70
1 parent e9554a2 commit 38bf1f7

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { ServerUserData } from '@cord-sdk/types';
2+
import type { Request, Response } from 'express';
3+
import { fetchCordRESTApi } from 'src/server/fetchCordRESTApi';
4+
5+
export async function handleAddChannel(req: Request, res: Response) {
6+
const { user_id: requesterID } = (req as any).loginTokenData;
7+
const { channelName } = req.params;
8+
const { isPrivate } = req.body;
9+
10+
// If userIDs is sent, it's a private channel, which means we need to create
11+
// an org for its participants. If no userIDs are sent, it's a public channel
12+
// which means it will be associated with the clack_all org.
13+
14+
if (isPrivate) {
15+
await fetchCordRESTApi<
16+
Promise<{
17+
success: boolean;
18+
message: string;
19+
}>
20+
>(
21+
`organizations/${channelName}`,
22+
'PUT',
23+
JSON.stringify({
24+
name: channelName,
25+
members: [requesterID],
26+
}),
27+
);
28+
}
29+
30+
const existingChannels = (
31+
await fetchCordRESTApi<ServerUserData>(`users/all_channels_holder`, 'GET')
32+
).metadata;
33+
34+
const response = await fetchCordRESTApi<
35+
Promise<{
36+
success: boolean;
37+
message: string;
38+
}>
39+
>(
40+
`users/all_channels_holder`,
41+
'PUT',
42+
JSON.stringify({
43+
metadata: {
44+
[channelName]: isPrivate ? channelName : '',
45+
...existingChannels,
46+
},
47+
}),
48+
);
49+
50+
res.send({ success: response.success });
51+
}

src/server/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
handleAddOrgMember,
2525
handleRemoveOrgMember,
2626
} from 'src/server/handlers/orgMembers';
27+
import { handleAddChannel } from 'src/server/handlers/updateChannels';
2728

2829
const REPO_ROOT = path.join(__dirname, '..', '..');
2930
dotenv.config({ path: path.join(REPO_ROOT, '.env') });
@@ -88,6 +89,8 @@ function main() {
8889
wrapAsyncHandler(handleRemoveOrgMember),
8990
);
9091

92+
app.put('/channels/:channelName', wrapAsyncHandler(handleAddChannel));
93+
9194
// Catch errors and log them
9295
app.use(
9396
'/',

0 commit comments

Comments
 (0)