194 lines
7.5 KiB
TypeScript
194 lines
7.5 KiB
TypeScript
import { Injectable, Logger, BadRequestException } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
|
|
import { MembershipTier } from './entities/membership-tier.entity';
|
|
import { CustomerValue } from './entities/customer-value.entity';
|
|
import { Region } from './entities/region.entity';
|
|
import { TripPurpose } from './entities/trip-purpose.entity';
|
|
import { CabinClass } from './entities/cabin-class.entity';
|
|
import { PassengerType } from './entities/passenger-type.entity';
|
|
import { AncillaryPurchase } from './entities/ancillary-purchase.entity';
|
|
import { RevenueSegment } from './entities/revenue-segment.entity';
|
|
|
|
@Injectable()
|
|
export class MasterDataService {
|
|
private readonly logger = new Logger(MasterDataService.name);
|
|
|
|
constructor(
|
|
@InjectRepository(MembershipTier) private membershipTierRepo: Repository<MembershipTier>,
|
|
@InjectRepository(CustomerValue) private customerValueRepo: Repository<CustomerValue>,
|
|
@InjectRepository(Region) private regionRepo: Repository<Region>,
|
|
@InjectRepository(TripPurpose) private tripPurposeRepo: Repository<TripPurpose>,
|
|
@InjectRepository(CabinClass) private cabinClassRepo: Repository<CabinClass>,
|
|
@InjectRepository(PassengerType) private passengerTypeRepo: Repository<PassengerType>,
|
|
@InjectRepository(AncillaryPurchase) private ancillaryPurchaseRepo: Repository<AncillaryPurchase>,
|
|
@InjectRepository(RevenueSegment) private revenueSegmentRepo: Repository<RevenueSegment>,
|
|
) {}
|
|
|
|
// Helper to get the correct repository based on category
|
|
private getRepositoryByCategory(category: string): Repository<any> {
|
|
switch (category?.toUpperCase()) {
|
|
case 'MEMBERSHIP_TIER': return this.membershipTierRepo;
|
|
case 'CUSTOMER_VALUE': return this.customerValueRepo;
|
|
case 'REGION': return this.regionRepo;
|
|
case 'TRIP_PURPOSE': return this.tripPurposeRepo;
|
|
case 'CABIN_CLASS': return this.cabinClassRepo;
|
|
case 'PASSENGER_TYPE': return this.passengerTypeRepo;
|
|
case 'ANCILLARY_PURCHASE': return this.ancillaryPurchaseRepo;
|
|
case 'REVENUE_SEGMENT': return this.revenueSegmentRepo;
|
|
default:
|
|
throw new BadRequestException(`Invalid category: ${category}`);
|
|
}
|
|
}
|
|
|
|
async findAll(category: string): Promise<any[]> {
|
|
if (!category) {
|
|
throw new BadRequestException('Category is required');
|
|
}
|
|
|
|
const repo = this.getRepositoryByCategory(category);
|
|
return repo.find({
|
|
where: { isActive: true },
|
|
});
|
|
}
|
|
|
|
async findOne(category: string, id: string): Promise<any> {
|
|
const repo = this.getRepositoryByCategory(category);
|
|
const item = await repo.findOne({ where: { id } });
|
|
if (!item) {
|
|
throw new BadRequestException(`${category} with id ${id} not found`);
|
|
}
|
|
return item;
|
|
}
|
|
|
|
async create(category: string, data: any): Promise<any> {
|
|
const repo = this.getRepositoryByCategory(category);
|
|
const newItem = repo.create(data);
|
|
return repo.save(newItem);
|
|
}
|
|
|
|
async update(category: string, id: string, data: any): Promise<any> {
|
|
const repo = this.getRepositoryByCategory(category);
|
|
const item = await this.findOne(category, id);
|
|
Object.assign(item, data);
|
|
return repo.save(item);
|
|
}
|
|
|
|
async remove(category: string, id: string): Promise<void> {
|
|
const repo = this.getRepositoryByCategory(category);
|
|
const item = await this.findOne(category, id);
|
|
await repo.remove(item);
|
|
}
|
|
|
|
public async seedData() {
|
|
this.logger.log('Checking if separate master data tables need seeding...');
|
|
|
|
// MEMBERSHIP_TIER
|
|
if (await this.membershipTierRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'Platinum', value: 'platinum' },
|
|
{ label: 'Gold', value: 'gold' },
|
|
{ label: 'Silver', value: 'silver' },
|
|
{ label: 'Bronze', value: 'bronze' },
|
|
{ label: 'Basic', value: 'basic' },
|
|
{ label: 'Non-Member', value: 'non-member' },
|
|
];
|
|
await this.membershipTierRepo.save(this.membershipTierRepo.create(data));
|
|
}
|
|
|
|
// CUSTOMER_VALUE
|
|
if (await this.customerValueRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'High Value', value: 'high' },
|
|
{ label: 'Medium Value', value: 'medium' },
|
|
{ label: 'Low Value', value: 'low' },
|
|
];
|
|
await this.customerValueRepo.save(this.customerValueRepo.create(data));
|
|
}
|
|
|
|
// REGION
|
|
if (await this.regionRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'Global', value: 'global' },
|
|
{ label: 'Americas', value: 'americas' },
|
|
{ label: 'Europe', value: 'europe' },
|
|
{ label: 'Middle East', value: 'middle-east' },
|
|
{ label: 'Asia Pacific', value: 'asia-pacific' },
|
|
{ label: 'Africa', value: 'africa' },
|
|
];
|
|
await this.regionRepo.save(this.regionRepo.create(data));
|
|
}
|
|
|
|
// TRIP_PURPOSE
|
|
if (await this.tripPurposeRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'Business', value: 'business' },
|
|
{ label: 'Leisure', value: 'leisure' },
|
|
{ label: 'Corporate', value: 'corporate' },
|
|
{ label: 'Government', value: 'government' },
|
|
|
|
];
|
|
await this.tripPurposeRepo.save(this.tripPurposeRepo.create(data));
|
|
}
|
|
|
|
// CABIN_CLASS
|
|
if (await this.cabinClassRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'First Class', value: 'first-class' },
|
|
{ label: 'Business Class', value: 'business-class' },
|
|
{ label: 'Premium Economy', value: 'premium-economy' },
|
|
{ label: 'Economy', value: 'economy' },
|
|
];
|
|
await this.cabinClassRepo.save(this.cabinClassRepo.create(data));
|
|
}
|
|
|
|
// PASSENGER_TYPE
|
|
if (await this.passengerTypeRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'Adult', value: 'adult' },
|
|
{ label: 'Child', value: 'child' },
|
|
{ label: 'Infant', value: 'infant' },
|
|
{ label: 'Senior Citizen', value: 'senior' },
|
|
];
|
|
await this.passengerTypeRepo.save(this.passengerTypeRepo.create(data));
|
|
}
|
|
|
|
// ANCILLARY_PURCHASE
|
|
if (await this.ancillaryPurchaseRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'Preferred Seat', value: 'preferred-seat' },
|
|
{ label: 'Extra Legroom', value: 'extra-legroom' },
|
|
{ label: 'Wi-Fi', value: 'wi-fi' },
|
|
{ label: 'Lounge Access', value: 'lounge-access' },
|
|
{ label: 'Priority Boarding', value: 'priority-boarding' },
|
|
{ label: 'Fast Track', value: 'fast-track' },
|
|
{ label: 'Paid Meal', value: 'paid-meal' },
|
|
{ label: 'Special Meal', value: 'special-meal' },
|
|
{ label: 'Extra Baggage', value: 'extra-baggage' },
|
|
{ label: 'Upgrade Purchase', value: 'upgrade-purchase' },
|
|
{ label: 'Airport Transfer', value: 'airport-transfer' },
|
|
{ label: 'Chauffeur Service', value: 'chauffeur-service' },
|
|
{ label: 'Sports Equipment', value: 'sports-equipment' },
|
|
{ label: 'Musical Instrument', value: 'musical-instrument' },
|
|
{ label: 'In-flight Entertainment', value: 'in-flight-entertainment' },
|
|
{ label: 'Power Outlet', value: 'power-outlet' },
|
|
{ label: 'Carbon Offset', value: 'carbon-offset' },
|
|
];
|
|
await this.ancillaryPurchaseRepo.save(this.ancillaryPurchaseRepo.create(data));
|
|
}
|
|
|
|
// REVENUE_SEGMENT
|
|
if (await this.revenueSegmentRepo.count() === 0) {
|
|
const data = [
|
|
{ label: 'High Value', value: 'high' },
|
|
{ label: 'Medium Value', value: 'medium' },
|
|
{ label: 'Low Value', value: 'low' },
|
|
];
|
|
await this.revenueSegmentRepo.save(this.revenueSegmentRepo.create(data));
|
|
}
|
|
|
|
this.logger.log('All 8 master data tables seeded successfully!');
|
|
}
|
|
}
|