Skip to content

Commit

Permalink
fix query bug and provide injection of database
Browse files Browse the repository at this point in the history
  • Loading branch information
bahram1249 committed Feb 20, 2024
1 parent 3635fbc commit 7d82cf3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
2 changes: 2 additions & 0 deletions apps/e-commerce/src/admin/product/product.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { EAVEntityType } from '@rahino/database/models/eav/eav-entity-type.entit
import { InventoryModule } from '@rahino/ecommerce/inventory/inventory.module';
import { UserVendorModule } from '@rahino/ecommerce/user/vendor/user-vendor.module';
import { QueryFilterModule } from '@rahino/query-filter';
import { DatabaseModule } from '@rahino/database';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { QueryFilterModule } from '@rahino/query-filter';
InventoryModule,
UserVendorModule,
QueryFilterModule,
SequelizeModule,
],
controllers: [ProductController],
providers: [ProductService, ProductProfile],
Expand Down
33 changes: 25 additions & 8 deletions apps/e-commerce/src/admin/product/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
NotFoundException,
NotImplementedException,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Op, Sequelize } from 'sequelize';
import { InjectConnection, InjectModel } from '@nestjs/sequelize';
import { Op, Sequelize, Transaction } from 'sequelize';
import { GetProductDto, ProductDto } from './dto';
import { ECProduct } from '@rahino/database/models/ecommerce-eav/ec-product.entity';
import { QueryOptionsBuilder } from '@rahino/query-filter/sequelize-query-builder';
Expand Down Expand Up @@ -66,7 +66,9 @@ export class ProductService {
private readonly inventoryService: InventoryService,
private readonly userVendorService: UserVendorService,
private readonly inventoryStatusService: inventoryStatusService,
@Inject(emptyListFilter) private readonly listFilter: ListFilter,
@Inject(emptyListFilter)
private readonly listFilter: ListFilter,
@InjectConnection() private readonly sequelize: Sequelize,
private config: ConfigService,
) {}

Expand Down Expand Up @@ -154,6 +156,21 @@ export class ProductService {
required: false,
},
{
attributes: [
'id',
'productId',
'vendorId',
'colorId',
'guaranteeId',
'guaranteeMonthId',
'buyPrice',
'qty',
'onlyProvinceId',
'vendorAddressId',
'weight',
'inventoryStatusId',
'description',
],
model: ECInventory,
as: 'inventories',
where: {
Expand Down Expand Up @@ -193,6 +210,7 @@ export class ProductService {
as: 'onlyProvince',
},
{
attributes: ['id', 'vendorId', 'addressId'],
model: ECVendorAddress,
as: 'vendorAddress',
include: [
Expand Down Expand Up @@ -428,7 +446,9 @@ export class ProductService {
dto.inventories,
);

const transaction = await new Sequelize().transaction();
const transaction = await this.sequelize.transaction({
isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED,
});
let product: ECProduct = null;
try {
// map item to product
Expand Down Expand Up @@ -483,10 +503,7 @@ export class ProductService {
await transaction.commit();
} catch (error) {
await transaction.rollback();
console.log(error);
throw new InternalServerErrorException(
'transaction of creating product failed',
);
throw new InternalServerErrorException(error.message);
}

return {
Expand Down
1 change: 1 addition & 0 deletions apps/e-commerce/src/inventory/inventory-status.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class inventoryStatusService {
),
)
.filter({ inventoryStatusId: InventoryStatusEnum.available })
.transaction(transaction)
.build(),
);
if (inventoriesCount > 0) {
Expand Down
15 changes: 11 additions & 4 deletions apps/e-commerce/src/inventory/inventory-validation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export class InventoryValidationService {
new QueryOptionsBuilder()
.filter(
Sequelize.where(
Sequelize.fn('isnull', 'ECVariationPrices.required', 0),
Sequelize.fn(
'isnull',
Sequelize.col('ECVariationPrice.required'),
0,
),
{
[Op.eq]: 1,
},
Expand Down Expand Up @@ -95,9 +99,12 @@ export class InventoryValidationService {
const color = await this.colorRepository.findOne(
new QueryOptionsBuilder()
.filter(
Sequelize.where(Sequelize.fn('isnull', 'ECColor.isDeleted', 0), {
[Op.eq]: 0,
}),
Sequelize.where(
Sequelize.fn('isnull', Sequelize.col('ECColor.isDeleted'), 0),
{
[Op.eq]: 0,
},
),
)
.build(),
);
Expand Down
4 changes: 3 additions & 1 deletion libs/database/src/database.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ import { ECVendorAddress } from './models/ecommerce-eav/ec-vendor-address.entity
import { ECVariationPrice } from './models/ecommerce-eav/ec-variation-prices';
import { ECInventory } from './models/ecommerce-eav/ec-inventory.entity';
import { ECInventoryPrice } from './models/ecommerce-eav/ec-inventory-price.entity';
import { EAVEntityAttribute } from './models/eav/eav-entity-attribute.entity';

@Module({
imports: [
SequelizeModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
name: 'sequelize_default',
dialect: configService.get<Dialect>('DB_DIALECT'),
host: configService.get<string>('DB_HOST'),
port: configService.get<number>('DB_PORT'),
Expand Down Expand Up @@ -98,6 +100,7 @@ import { ECInventoryPrice } from './models/ecommerce-eav/ec-inventory-price.enti
EAVEntity,
EAVAttributeType,
EAVAttribute,
EAVEntityAttribute,
EAVAttributeValue,
EAVEntityAttributeValue,
EAVEntityPhoto,
Expand Down Expand Up @@ -168,6 +171,5 @@ import { ECInventoryPrice } from './models/ecommerce-eav/ec-inventory-price.enti
}),
],
// providers: [...databaseProviders],
// exports: [...databaseProviders],
})
export class DatabaseModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { EAVAttributeValue } from './eav-attribute-value';
export class EAVEntityAttributeValue extends Model {
@Column({
type: DataType.BIGINT,
autoIncrement: true,
primaryKey: true,
})
@ForeignKey(() => EAVEntity)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { FindAndCountOptions, Includeable, Op, WhereOptions } from 'sequelize';
import {
FindAndCountOptions,
Includeable,
LOCK,
Op,
Transaction,
WhereOptions,
} from 'sequelize';
import { Order, OrderCol } from '@rahino/query-filter';

export class QueryOptionsBuilder {
Expand All @@ -25,22 +32,12 @@ export class QueryOptionsBuilder {
if (!this.options.order) this.options.order = [];
const orders = JSON.parse(JSON.stringify(this.options.order));
if (isOrderCol(orderArg)) {
if (orderArg instanceof OrderCol) {
orders.push([orderArg.orderBy, orderArg.sortOrder]);
}
const orderCol = orderArg as OrderCol;
orders.push([orderCol.orderBy, orderCol.sortOrder]);
} else {
orders.push(orderArg);
}

// if (orderArg instanceof OrderCol && isOrderCol(orderArg)) {
// orders.push([orderArg.orderBy, orderArg.sortOrder]);
// } else {
// orders.push(orderArg);
// }

// } else {
// orders.push(order);
// }
this.options.order = orders;
return this;
}
Expand All @@ -63,6 +60,14 @@ export class QueryOptionsBuilder {
build(): Omit<FindAndCountOptions<any>, 'group'> {
return this.options;
}
transaction(transaction: Transaction) {
this.options.transaction = transaction;
return this;
}
lock(transactionLock: LOCK) {
this.options.lock = transactionLock;
return this;
}
}

function isOrderCol(x: any) {
Expand Down

0 comments on commit 7d82cf3

Please sign in to comment.