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

Commit

Permalink
♻️ Update domain module with methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent 10e7ab8 commit 385c93c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
14 changes: 10 additions & 4 deletions src/modules/domains/domains.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { DomainsService } from './domains.service';
export class DomainController {
constructor(private domainsService: DomainsService) {}

/** Create a new domain for a group */
@Post()
@AuditLog('create-domain')
@Scopes('group-{groupId}:write-domain-*')
Expand All @@ -37,6 +38,7 @@ export class DomainController {
return this.domainsService.createDomain(groupId, data);
}

/** Get domains for a group */
@Get()
@Scopes('group-{groupId}:read-domain-*')
async getAll(
Expand All @@ -56,25 +58,28 @@ export class DomainController {
});
}

/** Read a domain for a group */
@Get(':id')
@Scopes('group-{groupId}:read-domain-{id}')
async get(
@Param('groupId', ParseIntPipe) groupId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Domain>> {
return this.domainsService.getDomain(groupId, Number(id));
return this.domainsService.getDomain(groupId, id);
}

/** Delete a domain for a group */
@Delete(':id')
@AuditLog('delete-domain')
@Scopes('group-{groupId}:delete-domain-{id}')
async remove(
@Param('groupId', ParseIntPipe) groupId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<Domain>> {
return this.domainsService.deleteDomain(groupId, Number(id));
return this.domainsService.deleteDomain(groupId, id);
}

/** Verify a domain using TXT record */
@Post(':id/verify/txt')
@AuditLog('verify-domain-txt')
@Scopes('group-{groupId}:write-domain-{id}')
Expand All @@ -84,11 +89,12 @@ export class DomainController {
): Promise<Expose<Domain>> {
return this.domainsService.verifyDomain(
groupId,
Number(id),
id,
DOMAIN_VERIFICATION_TXT,
);
}

/** Verify a domain using HTML file upload */
@Post(':id/verify/html')
@AuditLog('verify-domain-html')
@Scopes('group-{groupId}:write-domain-{id}')
Expand All @@ -98,7 +104,7 @@ export class DomainController {
): Promise<Expose<Domain>> {
return this.domainsService.verifyDomain(
groupId,
Number(id),
id,
DOMAIN_VERIFICATION_HTML,
);
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/domains/domains.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import { DomainsService } from './domains.service';
imports: [PrismaModule, TokensModule, DnsModule, ConfigModule],
controllers: [DomainController],
providers: [DomainsService],
exports: [DomainsService],
})
export class DomainsModule {}
47 changes: 28 additions & 19 deletions src/modules/domains/domains.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ConfigService } from '@nestjs/config';
import type { Prisma } from '@prisma/client';
import { Domain } from '@prisma/client';
import got from 'got';
import { Configuration } from '../../config/configuration.interface';
import { URL } from 'url';
import {
DOMAIN_NOT_FOUND,
Expand All @@ -20,7 +19,10 @@ import { DnsService } from '../../providers/dns/dns.service';
import { Expose } from '../../providers/prisma/prisma.interface';
import { PrismaService } from '../../providers/prisma/prisma.service';
import { TokensService } from '../../providers/tokens/tokens.service';
import { DOMAIN_VERIFICATION_TXT } from './domains.constants';
import {
DOMAIN_VERIFICATION_HTML,
DOMAIN_VERIFICATION_TXT,
} from './domains.constants';
import { DomainVerificationMethods } from './domains.interface';

@Injectable()
Expand All @@ -46,7 +48,7 @@ export class DomainsService {
)
)
throw new BadRequestException(INVALID_DOMAIN);
const verificationCode = this.tokensService.generateUuid();
const verificationCode = await this.tokensService.generateRandomString();
const currentProfilePicture = await this.prisma.group.findUnique({
where: { id: groupId },
select: { profilePictureUrl: true },
Expand Down Expand Up @@ -88,14 +90,18 @@ export class DomainsService {
},
): Promise<Expose<Domain>[]> {
const { skip, take, cursor, where, orderBy } = params;
const domains = await this.prisma.domain.findMany({
skip,
take,
cursor,
where: { ...where, group: { id: groupId } },
orderBy,
});
return domains.map((group) => this.prisma.expose<Domain>(group));
try {
const domains = await this.prisma.domain.findMany({
skip,
take,
cursor,
where: { ...where, group: { id: groupId } },
orderBy,
});
return domains.map((group) => this.prisma.expose<Domain>(group));
} catch (error) {
return [];
}
}

async getDomain(groupId: number, id: number): Promise<Expose<Domain>> {
Expand All @@ -111,29 +117,32 @@ export class DomainsService {
async verifyDomain(
groupId: number,
id: number,
method: DomainVerificationMethods,
method?: DomainVerificationMethods,
): Promise<Expose<Domain>> {
const domain = await this.prisma.domain.findUnique({
where: { id },
});
if (!domain) throw new NotFoundException(DOMAIN_NOT_FOUND);
if (domain.groupId !== groupId)
throw new UnauthorizedException(UNAUTHORIZED_RESOURCE);
if (method === DOMAIN_VERIFICATION_TXT) {

if (method === DOMAIN_VERIFICATION_TXT || !method) {
const txtRecords = await this.dnsService.lookup(domain.domain, 'TXT');
if (JSON.stringify(txtRecords).includes(domain.verificationCode)) {
await this.prisma.domain.update({
where: { id },
data: { isVerified: true },
});
} else throw new BadRequestException(DOMAIN_NOT_VERIFIED);
} else {
} else if (method) throw new BadRequestException(DOMAIN_NOT_VERIFIED);
}

if (method === DOMAIN_VERIFICATION_HTML || !method) {
let verified = false;
try {
const { body } = await got(
`http://${domain.domain}/.well-known/${this.configService.get<
Configuration['meta']['domainVerificationFile']
>('meta.domainVerificationFile' ?? 'staart-verify.txt')}`,
`http://${domain.domain}/.well-known/${this.configService.get<string>(
'meta.domainVerificationFile' ?? 'staart-verify.txt',
)}`,
);
verified = body.includes(domain.verificationCode);
} catch (error) {}
Expand All @@ -142,7 +151,7 @@ export class DomainsService {
where: { id },
data: { isVerified: true },
});
} else throw new BadRequestException(DOMAIN_NOT_VERIFIED);
} else if (method) throw new BadRequestException(DOMAIN_NOT_VERIFIED);
}
return domain;
}
Expand Down

0 comments on commit 385c93c

Please sign in to comment.