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

Commit

Permalink
♻️ Update memberships module
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent 7398bc8 commit fd347ee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
15 changes: 8 additions & 7 deletions src/modules/memberships/memberships-group.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MembershipsService } from './memberships.service';
export class GroupMembershipController {
constructor(private membershipsService: MembershipsService) {}

/** Add a member to a group */
@Post()
@AuditLog('add-membership')
@Scopes('group-{groupId}:write-membership-*')
Expand All @@ -39,6 +40,7 @@ export class GroupMembershipController {
return this.membershipsService.createGroupMembership(ip, groupId, data);
}

/** Get memberships for a group */
@Get()
@Scopes('group-{groupId}:read-membership-*')
async getAll(
Expand All @@ -58,15 +60,17 @@ export class GroupMembershipController {
});
}

/** Get a membership for a group */
@Get(':id')
@Scopes('group-{groupId}:read-membership-{id}')
async get(
@Param('groupId', ParseIntPipe) groupId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Membership>> {
return this.membershipsService.getGroupMembership(groupId, Number(id));
return this.membershipsService.getGroupMembership(groupId, id);
}

/** Update a membership for a group */
@Patch(':id')
@AuditLog('update-membership')
@Scopes('group-{groupId}:write-membership-{id}')
Expand All @@ -75,20 +79,17 @@ export class GroupMembershipController {
@Param('groupId', ParseIntPipe) groupId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Membership>> {
return this.membershipsService.updateGroupMembership(
groupId,
Number(id),
data,
);
return this.membershipsService.updateGroupMembership(groupId, id, data);
}

/** Remove a member from a group */
@Delete(':id')
@AuditLog('delete-membership')
@Scopes('group-{groupId}:delete-membership-{id}')
async remove(
@Param('groupId', ParseIntPipe) groupId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Membership>> {
return this.membershipsService.deleteGroupMembership(groupId, Number(id));
return this.membershipsService.deleteGroupMembership(groupId, id);
}
}
9 changes: 6 additions & 3 deletions src/modules/memberships/memberships-user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export class UserMembershipController {
return this.membershipsService.createUserMembership(userId, data);
}

/** Get memberships for a user */
@Get()
@Scopes('user-{userId}:read-membership')
@Scopes('user-{userId}:read-membership-*')
async getAll(
@Param('userId', ParseIntPipe) userId: number,
@Query('skip', OptionalIntPipe) skip?: number,
Expand All @@ -50,21 +51,23 @@ export class UserMembershipController {
});
}

/** Get a membership for a user */
@Get(':id')
@Scopes('user-{userId}:read-membership-{id}')
async get(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Membership>> {
return this.membershipsService.getUserMembership(userId, Number(id));
return this.membershipsService.getUserMembership(userId, id);
}

/** Delete a membership for a user */
@Delete(':id')
@Scopes('user-{userId}:delete-membership-{id}')
async remove(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Membership>> {
return this.membershipsService.deleteUserMembership(userId, Number(id));
return this.membershipsService.deleteUserMembership(userId, id);
}
}
2 changes: 2 additions & 0 deletions src/modules/memberships/memberships.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { MailModule } from '../../providers/mail/mail.module';
import { PrismaModule } from '../../providers/prisma/prisma.module';
import { TokensModule } from '../../providers/tokens/tokens.module';
import { ApiKeysModule } from '../api-keys/api-keys.module';
import { AuthModule } from '../auth/auth.module';
import { GroupsModule } from '../groups/groups.module';
Expand All @@ -17,6 +18,7 @@ import { MembershipsService } from './memberships.service';
AuthModule,
GroupsModule,
ApiKeysModule,
TokensModule,
],
controllers: [UserMembershipController, GroupMembershipController],
providers: [MembershipsService],
Expand Down
54 changes: 37 additions & 17 deletions src/modules/memberships/memberships.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,39 @@ import { ApiKeysService } from '../api-keys/api-keys.service';
import { AuthService } from '../auth/auth.service';
import { GroupsService } from '../groups/groups.service';
import { CreateMembershipInput } from './memberships.interface';
import { Configuration } from '../../config/configuration.interface';
import { TokensService } from '../../providers/tokens/tokens.service';

@Injectable()
export class MembershipsService {
private metaConfig = this.configService.get<Configuration['meta']>('meta');

constructor(
private prisma: PrismaService,
private auth: AuthService,
private email: MailService,
private configService: ConfigService,
private groupsService: GroupsService,
private apiKeyService: ApiKeysService,
private tokensService: TokensService,
) {}

async createUserMembership(
userId: number,
data: Omit<Prisma.GroupCreateInput, 'id'>,
) {
let id: number | undefined = undefined;
while (!id) {
id = Number(
`10${await this.tokensService.generateRandomString(6, 'numeric')}`,
);
const users = await this.prisma.user.findMany({ where: { id }, take: 1 });
if (users.length) id = undefined;
}
const created = await this.groupsService.createGroup(userId, {
...data,
id,
});
return created.memberships[0];
}

async getMemberships(params: {
skip?: number;
take?: number;
Expand All @@ -45,15 +63,19 @@ export class MembershipsService {
orderBy?: Prisma.MembershipOrderByInput;
}): Promise<Expose<Membership>[]> {
const { skip, take, cursor, where, orderBy } = params;
const memberships = await this.prisma.membership.findMany({
skip,
take,
cursor,
where,
orderBy,
include: { group: true, user: true },
});
return memberships.map((user) => this.prisma.expose<Membership>(user));
try {
const memberships = await this.prisma.membership.findMany({
skip,
take,
cursor,
where,
orderBy,
include: { group: true, user: true },
});
return memberships.map((user) => this.prisma.expose<Membership>(user));
} catch (error) {
return [];
}
}

async getUserMembership(
Expand Down Expand Up @@ -153,10 +175,6 @@ export class MembershipsService {
);
return this.prisma.expose<Membership>(membership);
}
async createUserMembership(userId: number, data: Prisma.GroupCreateInput) {
const created = await this.groupsService.createGroup(userId, data);
return created.memberships[0];
}

async createGroupMembership(
ipAddress: string,
Expand Down Expand Up @@ -186,7 +204,9 @@ export class MembershipsService {
data: {
name: user.name,
group: result.group.name,
link: `${this.metaConfig.frontendUrl}/groups/${groupId}`,
link: `${this.configService.get<string>(
'frontendUrl',
)}/groups/${groupId}`,
},
});
return this.prisma.expose<Membership>(result);
Expand Down

0 comments on commit fd347ee

Please sign in to comment.