mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 22:18:12 +00:00
Initial commit of edr-passenger-api alpha version
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { PromosService } from './promos.service';
|
||||
import { CreatePromotionDto } from './promos.dto';
|
||||
import { JwtGuard } from '../../common/jwt.guard';
|
||||
|
||||
@ApiTags('Promotions')
|
||||
@Controller('promos')
|
||||
export class PromosController {
|
||||
constructor(private service: PromosService) {}
|
||||
@Get() @ApiOperation({ summary: 'Get active promotions' }) getActive() { return this.service.getActive(); }
|
||||
@Get('validate/:code') @ApiOperation({ summary: 'Validate a promo code' }) validate(@Param('code') code: string) { return this.service.validate(code); }
|
||||
@Post() @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') @ApiOperation({ summary: 'Create promotion (admin)' }) create(@Body() dto: CreatePromotionDto) { return this.service.create(dto); }
|
||||
}
|
||||
13
apps/edr-passenger-api/src/modules/promos/promos.dto.ts
Normal file
13
apps/edr-passenger-api/src/modules/promos/promos.dto.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { IsString, IsOptional, IsInt } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class CreatePromotionDto {
|
||||
@ApiProperty({ example: 'Weekend Special' }) @IsString() title: string;
|
||||
@ApiPropertyOptional({ example: '15% off all routes' }) @IsOptional() @IsString() subtitle?: string;
|
||||
@ApiProperty({ example: 'WEEKEND15' }) @IsString() code: string;
|
||||
@ApiPropertyOptional({ example: 15 }) @IsOptional() @IsInt() percentOff?: number;
|
||||
@ApiPropertyOptional({ example: 5000 }) @IsOptional() @IsInt() amountOffMinor?: number;
|
||||
@ApiProperty({ example: '2026-12-31T23:59:59Z' }) @IsString() validUntil: string;
|
||||
@ApiPropertyOptional({ example: 'Book Now' }) @IsOptional() @IsString() ctaLabel?: string;
|
||||
@ApiPropertyOptional({ example: 'edr://search' }) @IsOptional() @IsString() deepLink?: string;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PromosController } from './promos.controller';
|
||||
import { PromosService } from './promos.service';
|
||||
|
||||
@Module({ controllers: [PromosController], providers: [PromosService] })
|
||||
export class PromosModule {}
|
||||
20
apps/edr-passenger-api/src/modules/promos/promos.service.ts
Normal file
20
apps/edr-passenger-api/src/modules/promos/promos.service.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../common/prisma.service';
|
||||
import { CreatePromotionDto } from './promos.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PromosService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
getActive() { return this.prisma.promotion.findMany({ where: { active: true, validUntil: { gte: new Date() } }, orderBy: { createdAt: 'desc' } }); }
|
||||
|
||||
async validate(code: string) {
|
||||
const promo = await this.prisma.promotion.findUnique({ where: { code } });
|
||||
if (!promo || !promo.active || promo.validUntil < new Date()) return { applicable: false, message: 'Promo code invalid or expired' };
|
||||
return { code: promo.code, percentOff: promo.percentOff, amountOffMinor: promo.amountOffMinor, validUntil: promo.validUntil, applicable: true, message: promo.percentOff ? `${promo.percentOff}% off` : `ETB ${((promo.amountOffMinor ?? 0) / 100).toFixed(2)} off` };
|
||||
}
|
||||
|
||||
create(dto: CreatePromotionDto) {
|
||||
return this.prisma.promotion.create({ data: { ...dto, validUntil: new Date(dto.validUntil) } });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user