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

Commit

Permalink
♻️ Update email module with imports
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent 385c93c commit 670e7b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/modules/emails/emails.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { EmailsService } from './emails.service';
export class EmailController {
constructor(private emailsService: EmailsService) {}

/** Create a new email for a user */
@Post()
@Scopes('user-{userId}:write-email-*')
async create(
Expand All @@ -31,6 +32,7 @@ export class EmailController {
return this.emailsService.createEmail(userId, data);
}

/** Get emails for a user */
@Get()
@Scopes('user-{userId}:read-email-*')
async getAll(
Expand All @@ -50,21 +52,23 @@ export class EmailController {
});
}

/** Get an email for a user */
@Get(':id')
@Scopes('user-{userId}:read-email-{id}')
async get(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Email>> {
return this.emailsService.getEmail(userId, Number(id));
return this.emailsService.getEmail(userId, id);
}

/** Delete an email for a user */
@Delete(':id')
@Scopes('user-{userId}:delete-email-{id}')
async remove(
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Email>> {
return this.emailsService.deleteEmail(userId, Number(id));
return this.emailsService.deleteEmail(userId, id);
}
}
4 changes: 4 additions & 0 deletions src/modules/emails/emails.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { TwilioModule } from '../../providers/twilio/twilio.module';
import { UsersService } from '../users/users.service';
import { EmailController } from './emails.controller';
import { EmailsService } from './emails.service';
import { S3Module } from '../../providers/s3/s3.module';
import { ApiKeysModule } from '../api-keys/api-keys.module';

@Module({
imports: [
Expand All @@ -20,6 +22,8 @@ import { EmailsService } from './emails.service';
TwilioModule,
PwnedModule,
TokensModule,
S3Module,
ApiKeysModule,
],
controllers: [EmailController],
providers: [
Expand Down
20 changes: 12 additions & 8 deletions src/modules/emails/emails.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ export class EmailsService {
},
): Promise<Expose<Email>[]> {
const { skip, take, cursor, where, orderBy } = params;
const emails = await this.prisma.email.findMany({
skip,
take,
cursor,
where: { ...where, user: { id: userId } },
orderBy,
});
return emails.map((user) => this.prisma.expose<Email>(user));
try {
const emails = await this.prisma.email.findMany({
skip,
take,
cursor,
where: { ...where, user: { id: userId } },
orderBy,
});
return emails.map((user) => this.prisma.expose<Email>(user));
} catch (error) {
return [];
}
}

async getEmail(userId: number, id: number): Promise<Expose<Email>> {
Expand Down

0 comments on commit 670e7b3

Please sign in to comment.