Skip to content

Commit e5ffcca

Browse files
authored
feat(api): add organizations api (#32)
1 parent 14f7178 commit e5ffcca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2086
-520
lines changed

modules/libs/domain/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const AppName = "Vidya"
1+
export const AppName = "Vidya"
2+
export * from './permissions'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const PermissionKeys = [
2+
// Organizations
3+
'orgs:create',
4+
'orgs:read',
5+
'orgs:update',
6+
'orgs:delete',
7+
8+
// Roles
9+
'roles:create',
10+
'roles:read',
11+
'roles:update',
12+
'roles:delete',
13+
] as const;
14+
15+
export type PermissionKey = typeof PermissionKeys[number];

modules/libs/entities/role.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as domain from '@vidya/domain';
12
import { Entity, Column, PrimaryGeneratedColumn, OneToMany, OneToOne } from 'typeorm';
23
import { UserRole } from './userRole';
34
import { School } from './school';
@@ -27,7 +28,7 @@ export class Role {
2728
school: School;
2829

2930
@Column('varchar', { array: true, nullable: true })
30-
permissions: string[];
31+
permissions: domain.PermissionKey[];
3132

3233
@OneToMany(() => UserRole, userRole => userRole.role)
3334
public userRoles: UserRole[];

modules/libs/protocol/auth.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import * as domain from '@vidya/domain';
2+
import { OtpType } from "./otp";
3+
14
/* -------------------------------------------------------------------------- */
25
/* One Time Password */
36
/* -------------------------------------------------------------------------- */
47

5-
import { OtpType } from "./otp";
6-
78
/**
89
* Request to get OTP for the specified destination.
910
*/
@@ -38,6 +39,7 @@ export interface JwtToken {
3839
}
3940

4041
export interface AccessToken extends JwtToken {
42+
permissions: UserPermission[];
4143
}
4244

4345
export interface RefreshToken extends JwtToken {
@@ -101,21 +103,18 @@ export interface LogOutResponse {
101103
* @remarks Used short names for the properties to reduce the size of a JWT token.
102104
*/
103105
export type UserPermission = {
104-
/* Organization ID */
106+
/**
107+
* Organization ID
108+
*/
105109
oid: string,
106110

107111
/* School ID */
108112
sid?: string,
109113

110114
/* Permissions */
111-
p: string[]
115+
p: domain.PermissionKey[]
112116
}
113117

114-
/**
115-
* List of permissions.
116-
*/
117-
export type UserPermissions = UserPermission[];
118-
119118
/* -------------------------------------------------------------------------- */
120119
/* Profile */
121120
/* -------------------------------------------------------------------------- */

modules/libs/protocol/crud.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* -------------------------------------------------------------------------- */
2+
/* Creaate */
3+
/* -------------------------------------------------------------------------- */
4+
5+
/**
6+
* Request to create an item.
7+
*/
8+
export type CreateItemRequest<TItemType> = TItemType;
9+
10+
/**
11+
* Response to creating an item.
12+
*/
13+
export type CreateItemResponse<TIdentityType> = {
14+
id: TIdentityType;
15+
}
16+
17+
/* -------------------------------------------------------------------------- */
18+
/* Read */
19+
/* -------------------------------------------------------------------------- */
20+
21+
/**
22+
* Generic response for retrieving a list of
23+
* items of a certain type.
24+
*/
25+
export type GetItemsListResponse<TItemType> = {
26+
items: TItemType[];
27+
}
28+
29+
/**
30+
* Generic response for retrieving a single item of
31+
* a certain type.
32+
*/
33+
export type GetItemResponse<TItemType> = TItemType;
34+
35+
/* -------------------------------------------------------------------------- */
36+
/* Update */
37+
/* -------------------------------------------------------------------------- */
38+
39+
/**
40+
* Request to update an item.
41+
*/
42+
export type UpdateItemRequest<TItemType> = Partial<TItemType>;
43+
44+
/**
45+
* Response to updating an item.
46+
*/
47+
export type UpdateItemResponse<TItemType> = Partial<TItemType>;
48+
49+
/* -------------------------------------------------------------------------- */
50+
/* Delete */
51+
/* -------------------------------------------------------------------------- */
52+
53+
/**
54+
* Request to delete an item.
55+
*/
56+
export type DeleteItemResponse = {
57+
success: boolean;
58+
}

modules/libs/protocol/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './error'
33
export * from './otp'
44
export * from './routes'
55
export * from './roles'
6-
export * from './userRoles'
6+
export * from './userRoles'
7+
export * from './organizations'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as crud from './crud'
2+
3+
/* -------------------------------------------------------------------------- */
4+
/* Models */
5+
/* -------------------------------------------------------------------------- */
6+
7+
export type OrganizationSummary = {
8+
id: string;
9+
name: string;
10+
}
11+
12+
export type OrganizationDetails = OrganizationSummary & {
13+
}
14+
15+
/* -------------------------------------------------------------------------- */
16+
/* Read */
17+
/* -------------------------------------------------------------------------- */
18+
19+
export type GetOrganizationsResponse =
20+
crud.GetItemsListResponse<OrganizationSummary>;
21+
22+
export type GetOrganizationResponse =
23+
crud.GetItemResponse<OrganizationDetails>;
24+
25+
/* -------------------------------------------------------------------------- */
26+
/* Update */
27+
/* -------------------------------------------------------------------------- */
28+
29+
export type UpdateOrganizationRequest =
30+
crud.UpdateItemRequest<Omit<OrganizationDetails, 'id'>>;
31+
32+
export type UpdateOrganizationResponse =
33+
crud.UpdateItemResponse<OrganizationDetails>;
34+
35+
/* -------------------------------------------------------------------------- */
36+
/* Delete */
37+
/* -------------------------------------------------------------------------- */
38+
39+
export type DeleteOrganizationResponse =
40+
crud.DeleteItemResponse;

modules/libs/protocol/routes.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ export const Routes = (baseUrl: string = '') => ({
1111
otp: {
1212
root: () => `${baseUrl}/auth/otp`,
1313
},
14-
org: {
14+
edu: {
15+
org: {
16+
find: () => `${baseUrl}/edu/organizations`,
17+
get: (id: string) => `${baseUrl}/edu/organizations/${id}`,
18+
create: () => `${baseUrl}/edu/organizations`,
19+
update: (id: string) => `${baseUrl}/edu/organizations/${id}`,
20+
delete: (id: string) => `${baseUrl}/edu/organizations/${id}`,
21+
},
1522
roles: {
16-
find: () => `${baseUrl}/org/roles`,
17-
get: (id: string) => `${baseUrl}/org/roles/${id}`,
18-
create: () => `${baseUrl}/org/roles`,
19-
update: (id: string) => `${baseUrl}/org/roles/${id}`,
20-
delete: (id: string) => `${baseUrl}/org/roles/${id}`,
23+
find: () => `${baseUrl}/edu/roles`,
24+
get: (id: string) => `${baseUrl}/edu/roles/${id}`,
25+
create: () => `${baseUrl}/edu/roles`,
26+
update: (id: string) => `${baseUrl}/edu/roles/${id}`,
27+
delete: (id: string) => `${baseUrl}/edu/roles/${id}`,
2128
},
22-
userRoles: {
23-
all: (userId: string) => `${baseUrl}/org/users/${userId}/roles`,
24-
create: (userId: string) => `${baseUrl}/org/users/${userId}/roles`,
25-
delete: (userId: string, roleId: string) => `${baseUrl}/org/users/${userId}/roles/${roleId}`,
26-
}
29+
user: (userId: string) => ({
30+
roles: {
31+
all: () => `${baseUrl}/edu/users/${userId}/roles`,
32+
create: () => `${baseUrl}/edu/users/${userId}/roles`,
33+
delete: (roleId: string) => `${baseUrl}/edu/users/${userId}/roles/${roleId}`,
34+
}
35+
}),
2736
}
2837
})

modules/services/api/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"eslint-config-prettier": "^9.0.0",
5555
"eslint-plugin-prettier": "^5.0.0",
5656
"eslint-plugin-simple-import-sort": "^12.1.1",
57+
"faker-js": "^1.0.0",
5758
"jest": "^29.5.0",
5859
"prettier": "^3.0.0",
5960
"source-map-support": "^0.5.21",
@@ -79,6 +80,12 @@
7980
"**/*.(t|j)s"
8081
],
8182
"coverageDirectory": "../coverage",
82-
"testEnvironment": "node"
83+
"testEnvironment": "node",
84+
"moduleNameMapper": {
85+
"^@vidya/api/(.*)$": "<rootDir>/$1",
86+
"^@vidya/protocol$": "<rootDir>/../../../libs/protocol",
87+
"^@vidya/entities$": "<rootDir>/../../../libs/entities",
88+
"^@vidya/domain$": "<rootDir>/../../../libs/domain"
89+
}
8390
}
8491
}

modules/services/api/src/app.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
import { Entities, Organization } from '@vidya/entities';
1313

1414
import { AuthModule } from './auth/auth.module';
15-
import { OrgModule } from './org/org.module';
16-
import { OrganizationsService } from './organizations.service';
15+
import { EduModule } from './edu/edu.module';
16+
import { OrganizationsService } from './edu/services/organizations.service';
1717

1818
@Module({
1919
imports: [
@@ -42,7 +42,7 @@ import { OrganizationsService } from './organizations.service';
4242
}),
4343
TypeOrmModule.forFeature([Organization]),
4444
AuthModule,
45-
OrgModule,
45+
EduModule,
4646
],
4747
controllers: [],
4848
providers: [OrganizationsService],

0 commit comments

Comments
 (0)