Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 979d0fd

Browse files
✨ Patch and create organizations
1 parent 6f4cd4f commit 979d0fd

File tree

7 files changed

+95
-15
lines changed

7 files changed

+95
-15
lines changed

src/interfaces/enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum MembershipRole {
2+
OWNER = 1,
23
ADMIN = 2,
34
MANAGER = 3,
45
MEMBER = 4,
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface Organization {
2-
id: number;
2+
id?: number;
33
name?: string;
44
invitationDomain?: string;
5-
createdAt: Date;
6-
updatedAt: Date;
5+
createdAt?: Date;
6+
updatedAt?: Date;
77
}

src/rest/auth.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export const login = async (
4646
export const register = async (
4747
user: User,
4848
email?: string,
49-
organizationId?: number
49+
organizationId?: number,
50+
role?: MembershipRole
5051
) => {
5152
// Create user
5253
const result = <InsertResult>await createUser(user);
@@ -61,11 +62,11 @@ export const register = async (
6162
await updateUser(userId, { primaryEmail: emailId });
6263
await sendEmailVerification(emailId, email, user);
6364
}
64-
if (organizationId) {
65+
if (organizationId && role) {
6566
await createMembership({
6667
userId,
6768
organizationId,
68-
role: MembershipRole.ADMIN
69+
role
6970
});
7071
}
7172
return { created: true };

src/rest/organization.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Organization } from "../interfaces/tables/organization";
2+
import { createOrganization, updateOrganization } from "../crud/organization";
3+
import { InsertResult } from "../interfaces/mysql";
4+
import { createMembership, getUserOrganizationId } from "../crud/membership";
5+
import {
6+
MembershipRole,
7+
ErrorCode,
8+
UserRole,
9+
EventType
10+
} from "../interfaces/enum";
11+
import { getUser } from "../crud/user";
12+
import { createEvent } from "../crud/event";
13+
import { Locals } from "../interfaces/general";
14+
15+
export const newOrganizationForUser = async (
16+
userId: number,
17+
organization: Organization,
18+
locals: Locals
19+
) => {
20+
const org = <InsertResult>await createOrganization(organization);
21+
await createMembership({
22+
organizationId: org.insertId,
23+
userId,
24+
role: MembershipRole.OWNER
25+
});
26+
await createEvent(
27+
{
28+
userId,
29+
type: EventType.ORGANIZATION_CREATED,
30+
data: { id: org.insertId }
31+
},
32+
locals
33+
);
34+
return;
35+
};
36+
37+
export const updateOrganizationForUser = async (
38+
userId: number,
39+
organizationId: number,
40+
data: Organization,
41+
locals: Locals
42+
) => {
43+
const user = await getUser(userId);
44+
const userOrganizationId = await getUserOrganizationId(user);
45+
if (organizationId == userOrganizationId || user.role == UserRole.ADMIN) {
46+
await updateOrganization(organizationId, data);
47+
await createEvent(
48+
{
49+
userId,
50+
type: EventType.ORGANIZATION_UPDATED,
51+
data: { id: organizationId, data }
52+
},
53+
locals
54+
);
55+
return;
56+
}
57+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
58+
};

src/routes/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Application } from "express";
22
import asyncHandler from "express-async-handler";
3-
import { routeUserMe, routeUserPut, routeUserId } from "./users";
3+
import { routeUserPut, routeUserId } from "./users";
44
import { routeEmailVerify, routeEmailAdd, routeEmailDelete } from "./emails";
5-
import { routeOrganizationCreate } from "./organizations";
5+
import { routeOrganizationCreate, routeOrganizationUpdate } from "./organizations";
66
import { authHandler } from "../helpers/middleware";
77
import {
88
routeAuthVerifyToken,
@@ -36,7 +36,6 @@ const routesAuth = (app: Application) => {
3636
};
3737

3838
const routesUser = (app: Application) => {
39-
app.get("/users/me", authHandler, asyncHandler(routeUserMe));
4039
app.get("/users/:id", authHandler, asyncHandler(routeUserId));
4140
app.put("/users", asyncHandler(routeUserPut));
4241
};
@@ -49,4 +48,5 @@ const routesEmail = (app: Application) => {
4948

5049
const routesOrganization = (app: Application) => {
5150
app.put("/organizations", authHandler, asyncHandler(routeOrganizationCreate));
51+
app.patch("/organizations/:id", authHandler, asyncHandler(routeOrganizationUpdate));
5252
};

src/routes/organizations.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
import { Request, Response } from "express";
2+
import {
3+
newOrganizationForUser,
4+
updateOrganizationForUser
5+
} from "../rest/organization";
6+
import { ErrorCode } from "../interfaces/enum";
27

38
export const routeOrganizationCreate = async (req: Request, res: Response) => {
9+
const name = req.body.name;
10+
const invitationDomain = req.body.invitationDomain;
11+
await newOrganizationForUser(
12+
res.locals.token.id,
13+
{ name, invitationDomain },
14+
res.locals
15+
);
16+
res.json({ success: true });
17+
};
18+
19+
export const routeOrganizationUpdate = async (req: Request, res: Response) => {
20+
const id = req.params.id;
21+
if (!id) throw new Error(ErrorCode.MISSING_FIELD);
22+
await updateOrganizationForUser(
23+
res.locals.token.id,
24+
id,
25+
req.body,
26+
res.locals
27+
);
428
res.json({ success: true });
529
};

src/routes/users.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { Request, Response } from "express";
22
import { register } from "../rest/auth";
3-
import { getUser } from "../crud/user";
43
import { getUserFromId } from "../rest/user";
54
import { ErrorCode } from "../interfaces/enum";
65

7-
export const routeUserMe = async (req: Request, res: Response) => {
8-
res.json({ user: await getUser(res.locals.token.id) });
9-
};
10-
116
export const routeUserId = async (req: Request, res: Response) => {
12-
const id = req.body.id || req.params.id;
7+
let id = req.body.id || req.params.id;
8+
if (id === "me") id = res.locals.token.id;
139
if (!id) throw new Error(ErrorCode.MISSING_FIELD);
1410
res.json({ user: await getUserFromId(id, res.locals.token.id) });
1511
};

0 commit comments

Comments
 (0)