Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling organizations fix #142

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 11 additions & 37 deletions server/src/organization/organization.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ import {
UpdateOrganizationDataDto,
} from './organization.dto';
import { OrganizationService } from './organization.service';
import { UserService } from '../user/user.service';

@WebSocketGateway({ cors: true })
@UseGuards(UserGuard)
export class OrganizationGateway {
constructor(
private clientService: ClientService,
private orgService: OrganizationService,
private userService: UserService,
) {}

/**
* Requests organization based on RequestOrganizationDataDto. Subscribes and emits the requested information
* @param user The calling user
* @param data The data to be requested
*/
@SubscribeMessage('requestOrganizationData')
async requestOrganizationData(
@CallingUser() user: User,
Expand All @@ -48,6 +51,12 @@ export class OrganizationGateway {
}
}

/**
* Updates organization based on UpdateOrganizationDataDto. Subscribes and emits data
* @param user The calling user
* @param data The data to be updated
* @returns Emits update organization
*/
@SubscribeMessage('updateOrganizationData')
async updateOrganizationData(
@CallingUser() user: User,
Expand All @@ -73,39 +82,4 @@ export class OrganizationGateway {
await this.orgService.emitUpdateOrganizationData(org, false);
}
}

@SubscribeMessage('addManager')
async addManager(
@CallingUser() user: User,
@MessageBody() data: { email: string; organizationId: string },
) {
if (!user.administrator) return;

await this.orgService.addManager(user, data.email, data.organizationId);

const org = await this.orgService.getOrganizationById(data.organizationId);
await this.orgService.emitUpdateOrganizationData(org, false);

const manager = await this.userService.byEmail(data.email);
await this.userService.emitUpdateUserData(
manager,
false,
false,
true,
user,
);
}

@SubscribeMessage('joinOrganization')
async joinOrganization(
@CallingUser() user: User,
@MessageBody() data: { accessCode: string },
) {
await this.orgService.joinOrganization(user, data.accessCode);

const org = await this.orgService.getOrganizationByCode(data.accessCode);
await this.orgService.emitUpdateOrganizationData(org, false);

await this.userService.emitUpdateUserData(user, false, false, true);
}
}
10 changes: 6 additions & 4 deletions server/src/organization/organization.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { ClientModule } from '../client/client.module';
import { PrismaModule } from '../prisma/prisma.module';
import { OrganizationService } from './organization.service';
import { OrganizationGateway } from './organization.gateway';
import { AuthModule } from '../auth/auth.module';

@Module({
imports: [PrismaModule, ClientModule],
exports: [OrganizationService],
providers: [OrganizationService],
imports: [PrismaModule, ClientModule, forwardRef(() => AuthModule)],
exports: [OrganizationService, OrganizationGateway],
providers: [OrganizationService, OrganizationGateway],
})
export class OrganizationModule {}
5 changes: 5 additions & 0 deletions server/src/user/user.gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UserService } from './user.service';
import { GroupService } from '../group/group.service';
import { EventService } from '../event/event.service';
import { AuthService } from '../auth/auth.service';
import { OrganizationService } from '../organization/organization.service';

describe('UserGateway', () => {
let gateway: UserGateway;
Expand Down Expand Up @@ -33,6 +34,10 @@ describe('UserGateway', () => {
useValue: null,
provide: AuthService,
},
{
useValue: null,
provide: OrganizationService,
},
],
}).compile();

Expand Down
37 changes: 37 additions & 0 deletions server/src/user/user.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from './user.dto';
import { UserService } from './user.service';
import { readFileSync } from 'fs';
import { OrganizationService } from '../organization/organization.service';

const majors = readFileSync('/app/server/src/user/majors.txt', 'utf8').split(
'\n',
Expand All @@ -46,6 +47,7 @@ export class UserGateway {
private userService: UserService,
private groupService: GroupService,
private eventService: EventService,
private orgService: OrganizationService,
) {}

private providerToAuthType(provider: string) {
Expand Down Expand Up @@ -234,6 +236,41 @@ export class UserGateway {
}
}

@SubscribeMessage('addManager')
async addManager(
@CallingUser() user: User,
@MessageBody() data: { email: string; organizationId: string },
) {
if (!user.administrator) return;

await this.orgService.addManager(user, data.email, data.organizationId);

const org = await this.orgService.getOrganizationById(data.organizationId);
await this.orgService.emitUpdateOrganizationData(org, false);

const manager = await this.userService.byEmail(data.email);
await this.userService.emitUpdateUserData(
manager,
false,
false,
true,
user,
);
}

@SubscribeMessage('joinOrganization')
async joinOrganization(
@CallingUser() user: User,
@MessageBody() data: { accessCode: string },
) {
await this.orgService.joinOrganization(user, data.accessCode);

const org = await this.orgService.getOrganizationByCode(data.accessCode);
await this.orgService.emitUpdateOrganizationData(org, false);

await this.userService.emitUpdateUserData(user, false, false, true);
}

@SubscribeMessage('closeAccount')
async closeAccount(
@CallingUser() user: User,
Expand Down
Loading