first commit

This commit is contained in:
2025-11-10 15:05:01 +05:30
commit 0230105958
35 changed files with 13428 additions and 0 deletions

View File

View File

@@ -0,0 +1,51 @@
// src/modules/host/dto/host.dto.ts
import { IsInt, IsOptional, IsString, IsBoolean, IsEmail } from 'class-validator';
export class CreateHostDto {
@IsString()
firstName: string;
@IsString()
lastName: string;
@IsEmail()
emailAddress: string;
@IsOptional()
@IsString()
isdCode?: string;
@IsOptional()
@IsString()
mobileNumber?: string;
@IsOptional()
@IsString()
userPasscode?: string;
@IsOptional()
@IsInt()
roleXid?: number;
@IsOptional()
@IsBoolean()
isActive?: boolean;
}
export class UpdateHostDto {
@IsOptional()
@IsString()
firstName?: string;
@IsOptional()
@IsString()
lastName?: string;
@IsOptional()
@IsEmail()
emailAddress?: string;
@IsOptional()
@IsBoolean()
isActive?: boolean;
}

View File

@@ -0,0 +1,68 @@
// src/modules/host/handler/host.handler.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from 'src/app.module';
import { HostService } from '../services/host.service';
import { APIGatewayProxyHandler } from 'aws-lambda';
import { CreateHostDto, UpdateHostDto } from '../dto/host.dto';
let app;
let hostService: HostService;
async function bootstrap() {
if (!app) {
const nestApp = await NestFactory.createApplicationContext(AppModule);
hostService = nestApp.get(HostService);
app = nestApp;
}
}
export const handler: APIGatewayProxyHandler = async (event) => {
await bootstrap();
const method = event.httpMethod;
const path = event.path;
const body = event.body ? JSON.parse(event.body) : {};
try {
if (method === 'POST' && path.endsWith('/host')) {
const host = await hostService.createHost(body as CreateHostDto);
return response(201, host);
}
if (method === 'GET' && path.endsWith('/host')) {
const hosts = await hostService.getAllHosts();
return response(200, hosts);
}
if (method === 'GET' && path.match(/\/host\/\d+$/)) {
const id = Number(path.split('/').pop());
const host = await hostService.getHostById(id);
return response(200, host);
}
if (method === 'PUT' && path.match(/\/host\/\d+$/)) {
const id = Number(path.split('/').pop());
const host = await hostService.updateHost(id, body as UpdateHostDto);
return response(200, host);
}
if (method === 'DELETE' && path.match(/\/host\/\d+$/)) {
const id = Number(path.split('/').pop());
const host = await hostService.deleteHost(id);
return response(200, { message: 'Host deleted successfully', host });
}
return response(404, { message: 'Not Found' });
} catch (error) {
console.error('Error:', error);
return response(500, { message: error.message || 'Internal Server Error' });
}
};
function response(statusCode: number, body: any) {
return {
statusCode,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
};
}

View File

@@ -0,0 +1,34 @@
// src/modules/host/services/host.service.ts
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../../common/database/prisma.service';
import { CreateHostDto, UpdateHostDto } from '../dto/host.dto';
@Injectable()
export class HostService {
constructor(private prisma: PrismaService) {}
async createHost(data: CreateHostDto) {
return this.prisma.user.create({ data });
}
async getAllHosts() {
return this.prisma.user.findMany({ where: { roleXid: 3 } });
}
async getHostById(id: number) {
const host = await this.prisma.user.findUnique({ where: { id } });
if (!host || host.roleXid !== 3) throw new Error('Host not found');
return host;
}
async updateHost(id: number, data: UpdateHostDto) {
return this.prisma.user.update({
where: { id },
data,
});
}
async deleteHost(id: number) {
return this.prisma.user.delete({ where: { id } });
}
}