feat(db): add database repositories
This commit is contained in:
36
src/database/productVersion.database.repository.ts
Normal file
36
src/database/productVersion.database.repository.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user