chore: migrate to pnpm workspaces monorepo with Turborepo
- Restructure directories: apps/api, apps/admin, apps/website - Add root pnpm-workspace.yaml, turbo.json, .prettierrc, .gitignore - Rename packages to @inkreach/api, @inkreach/admin, @inkreach/website - Add shared packages: packages/tsconfig, packages/shared-types - Add pnpm.onlyBuiltDependencies for native builds - Update docs: README.md, structs.md - All three projects build successfully
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Patch,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||
import { CountriesService } from './countries.service';
|
||||
import { CreateCountryDto } from './dto/create-country.dto';
|
||||
import { UpdateCountryDto } from './dto/update-country.dto';
|
||||
|
||||
@ApiTags('countries')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('countries')
|
||||
export class CountriesController {
|
||||
constructor(private readonly service: CountriesService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all countries' })
|
||||
findAll() {
|
||||
return this.service.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get one country' })
|
||||
findOne(@Param('id', ParseIntPipe) id: string) {
|
||||
return this.service.findOne(BigInt(id));
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create a country' })
|
||||
create(@Body() dto: CreateCountryDto) {
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Update a country' })
|
||||
update(
|
||||
@Param('id', ParseIntPipe) id: string,
|
||||
@Body() dto: UpdateCountryDto,
|
||||
) {
|
||||
return this.service.update(BigInt(id), dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete a country' })
|
||||
remove(@Param('id', ParseIntPipe) id: string) {
|
||||
return this.service.remove(BigInt(id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CountriesController } from './countries.controller';
|
||||
import { CountriesService } from './countries.service';
|
||||
|
||||
@Module({
|
||||
controllers: [CountriesController],
|
||||
providers: [CountriesService],
|
||||
exports: [CountriesService],
|
||||
})
|
||||
export class CountriesModule {}
|
||||
@@ -0,0 +1,115 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { CountriesService } from './countries.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('CountriesService', () => {
|
||||
let service: CountriesService;
|
||||
let prisma: PrismaService;
|
||||
const created: string[] = [];
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
providers: [CountriesService, PrismaService],
|
||||
}).compile();
|
||||
service = moduleRef.get(CountriesService);
|
||||
prisma = moduleRef.get(PrismaService);
|
||||
await prisma.onModuleInit();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (created.length) {
|
||||
// Delete goods/origin_goods first so we can remove the countries.
|
||||
await prisma.good.deleteMany({
|
||||
where: { country: { countryName: { in: created } } },
|
||||
});
|
||||
await prisma.position.deleteMany({
|
||||
where: { country: { countryName: { in: created } } },
|
||||
});
|
||||
await prisma.country.deleteMany({
|
||||
where: { countryName: { in: created } },
|
||||
});
|
||||
}
|
||||
await prisma.onModuleDestroy();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('creates and reads back a country', async () => {
|
||||
const name = `Test Country ${Date.now()}`;
|
||||
created.push(name);
|
||||
|
||||
const createdRow = await service.create({ countryName: name });
|
||||
expect(createdRow.countryName).toBe(name);
|
||||
|
||||
const fetched = await service.findOne(createdRow.id);
|
||||
expect(fetched.countryName).toBe(name);
|
||||
});
|
||||
|
||||
it('rejects duplicate names with ConflictException', async () => {
|
||||
const name = `Dup Country ${Date.now()}`;
|
||||
created.push(name);
|
||||
await service.create({ countryName: name });
|
||||
await expect(service.create({ countryName: name })).rejects.toBeInstanceOf(
|
||||
ConflictException,
|
||||
);
|
||||
});
|
||||
|
||||
it('throws NotFoundException for unknown id', async () => {
|
||||
await expect(service.findOne(BigInt(99999999))).rejects.toBeInstanceOf(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it('throws BadRequestException when deleting a country referenced by goods', async () => {
|
||||
const name = `Ref Country ${Date.now()}`;
|
||||
created.push(name);
|
||||
const country = await service.create({ countryName: name });
|
||||
|
||||
// Need a category + origin good to satisfy the foreign keys before
|
||||
// we can attach a good that references the country.
|
||||
const category = await prisma.category.create({
|
||||
data: { categoryName: `Cat ${Date.now()}` },
|
||||
});
|
||||
const originGood = await prisma.originGood.create({
|
||||
data: { sdsGoodId: `sds-${Date.now()}-${Math.random()}` },
|
||||
});
|
||||
|
||||
await prisma.good.create({
|
||||
data: {
|
||||
originGoodId: originGood.id,
|
||||
countryId: country.id,
|
||||
categoryId: category.id,
|
||||
goodName: 'sample',
|
||||
},
|
||||
});
|
||||
|
||||
await expect(service.remove(country.id)).rejects.toBeInstanceOf(
|
||||
BadRequestException,
|
||||
);
|
||||
|
||||
// cleanup
|
||||
await prisma.good.deleteMany({ where: { countryId: country.id } });
|
||||
await prisma.originGood.delete({ where: { id: originGood.id } });
|
||||
await prisma.category.delete({ where: { id: category.id } });
|
||||
});
|
||||
|
||||
it('updates fields and deletes when unreferenced', async () => {
|
||||
const name = `Upd Country ${Date.now()}`;
|
||||
created.push(name);
|
||||
const c = await service.create({ countryName: name });
|
||||
const updated = await service.update(c.id, { countryName: `${name}-v2` });
|
||||
expect(updated.countryName).toBe(`${name}-v2`);
|
||||
created[created.indexOf(name)] = `${name}-v2`;
|
||||
|
||||
await service.remove(c.id);
|
||||
const idx = created.indexOf(`${name}-v2`);
|
||||
if (idx !== -1) created.splice(idx, 1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,100 @@
|
||||
import { Prisma } from '@prisma/client';
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateCountryDto } from './dto/create-country.dto';
|
||||
import { UpdateCountryDto } from './dto/update-country.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CountriesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
findAll() {
|
||||
return this.prisma.country.findMany({ orderBy: { id: 'asc' } });
|
||||
}
|
||||
|
||||
async findOne(id: bigint) {
|
||||
const country = await this.prisma.country.findUnique({ where: { id } });
|
||||
if (!country) {
|
||||
throw new NotFoundException(`Country ${id} not found`);
|
||||
}
|
||||
return country;
|
||||
}
|
||||
|
||||
async create(dto: CreateCountryDto) {
|
||||
try {
|
||||
return await this.prisma.country.create({
|
||||
data: {
|
||||
countryName: dto.countryName,
|
||||
countryIcon: dto.countryIcon ?? null,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
err.code === 'P2002'
|
||||
) {
|
||||
throw new ConflictException('Country name already exists');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async update(id: bigint, dto: UpdateCountryDto) {
|
||||
await this.findOne(id);
|
||||
try {
|
||||
return await this.prisma.country.update({
|
||||
where: { id },
|
||||
data: {
|
||||
countryName: dto.countryName,
|
||||
countryIcon: dto.countryIcon === undefined ? undefined : dto.countryIcon,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
err.code === 'P2002'
|
||||
) {
|
||||
throw new ConflictException('Country name already exists');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async remove(id: bigint) {
|
||||
await this.findOne(id);
|
||||
try {
|
||||
return await this.prisma.country.delete({ where: { id } });
|
||||
} catch (err) {
|
||||
if (this.isForeignKeyViolation(err)) {
|
||||
throw new BadRequestException(
|
||||
'Country is referenced by goods or positions and cannot be deleted',
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private isForeignKeyViolation(err: unknown): boolean {
|
||||
if (err instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
// P2003 = FK constraint violation
|
||||
return err.code === 'P2003';
|
||||
}
|
||||
// Fallback: Prisma sometimes surfaces FK violations as UnknownRequestError
|
||||
// when the constraint check happens server-side before the typed error
|
||||
// is mapped (e.g. cascading RESTRICT).
|
||||
if (err instanceof Prisma.PrismaClientUnknownRequestError) {
|
||||
const msg = err.message ?? '';
|
||||
return (
|
||||
msg.includes('foreign key constraint') ||
|
||||
msg.includes('RESTRICT') ||
|
||||
msg.includes('violates')
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class CreateCountryDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
countryName!: string;
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
countryIcon?: string;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class UpdateCountryDto {
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
countryName?: string;
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
countryIcon?: string | null;
|
||||
}
|
||||
Reference in New Issue
Block a user