@sourceloop/ctrl-plane-tenant-management-service
Version:
Tenant Management microservice for SaaS control plane
304 lines (292 loc) • 7.24 kB
text/typescript
import {inject, service} from '@loopback/core';
import {
Count,
CountSchema,
Filter,
repository,
Where,
} from '@loopback/repository';
import {
del,
get,
getModelSchemaRef,
HttpErrors,
param,
patch,
post,
put,
requestBody,
RestBindings,
} from '@loopback/rest';
import {
CONTENT_TYPE,
OPERATION_SECURITY_SPEC,
rateLimitKeyGenPublic,
STATUS_CODE,
} from '@sourceloop/core';
import {
authenticate,
AuthenticationBindings,
STRATEGY,
} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {CreateLeadDTO, Lead} from '../models';
import {PermissionKey} from '../permissions';
import {LeadRepository} from '../repositories';
import {OnboardingService} from '../services';
import {ratelimit} from 'loopback4-ratelimiter';
import {LEAD_TOKEN_VERIFIER} from '../keys';
import {LeadUserWithToken} from '../types';
import {VerifyLeadResponseDTO} from '../models/dtos/verify-lead-response-dto.model';
const basePath = '/leads';
const leadDescription = 'Lead model instance';
export class LeadController {
constructor(
public leadRepository: LeadRepository,
public onboarding: OnboardingService,
private readonly request: Request,
) {}
async create(
lead: Omit<CreateLeadDTO, 'isValidated' | 'addressId' | 'id'>,
): Promise<{[key: string]: string}> {
return this.onboarding.addLead(lead);
}
async validateLead(
.path.string('id') id: string,
leadUser: LeadUserWithToken,
): Promise<VerifyLeadResponseDTO> {
if (leadUser.id !== id) {
throw new HttpErrors.Unauthorized();
}
await this.leadRepository.updateById(leadUser.id, {
isValidated: true,
});
return new VerifyLeadResponseDTO({
id: leadUser.id,
token: leadUser.token,
});
}
async count(.where(Lead) where?: Where<Lead>): Promise<Count> {
return this.leadRepository.count(where);
}
async find(.filter(Lead) filter?: Filter<Lead>): Promise<Lead[]> {
return this.leadRepository.find(filter);
}
async updateAll(
lead: Lead,
.where(Lead) where?: Where<Lead>,
): Promise<Count> {
throw HttpErrors.MethodNotAllowed();
}
async findById(
.path.string('id') id: string,
.filter(Lead, {exclude: 'where'})
filter?: Filter<Lead>,
): Promise<Lead> {
return this.leadRepository.findById(id, filter);
}
async updateById(
.path.string('id') id: string,
lead: Lead,
): Promise<void> {
throw HttpErrors.MethodNotAllowed();
}
async replaceById(
.path.string('id') id: string,
lead: Lead,
): Promise<void> {
throw new HttpErrors.MethodNotAllowed();
}
async deleteById(.path.string('id') id: string): Promise<void> {
throw HttpErrors.MethodNotAllowed();
}
}