feat(db): add database repositories

This commit is contained in:
MastersGame
2026-11-05 23:00:48 +01:00
parent aa4262c45a
commit 60cd4cc1d6
6 changed files with 232 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { ProductVersion } from '../../prisma/generated/client';
import { ProductVersionCreateInput } from '../../prisma/generated/models/ProductVersion';
/** Database access to product versions (one per Hiboutik size), identified by their product and `hiboutikSizeId`. */
@Injectable()
export class ProductVersionDatabaseRepository {
constructor(private readonly prisma: PrismaService) {}
async getAll(): Promise<ProductVersion[]> {
return this.prisma.productVersion.findMany({
orderBy: { hiboutikSizeId: 'asc' },
});
}
/** Creates or updates the version of `productId` with the same `hiboutikSizeId`. */
async upsert(productId: string, productVersion: ProductVersionCreateInput): Promise<ProductVersion> {
return this.prisma.productVersion.upsert({
where: { productId_hiboutikSizeId: { productId: productId, hiboutikSizeId: productVersion.hiboutikSizeId } },
create: productVersion,
update: productVersion,
});
}
/**
* Deletes the versions of a product that no longer exist in Hiboutik.
* Order lines keep their own snapshot of the product, their link is set to null.
*/
async deleteMissing(productId: string, keptHiboutikSizeIds: number[]): Promise<number> {
const result = await this.prisma.productVersion.deleteMany({
where: { productId, hiboutikSizeId: { notIn: keptHiboutikSizeIds } },
});
return result.count;
}
}