"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateProductDetailStockQty1687413760311 = void 0;
const typeorm_1 = require("typeorm");
class UpdateProductDetailStockQty1687413760311 {
    async up(queryRunner) {
        await (0, typeorm_1.getConnection)().transaction(async (trx) => {
            await trx.query(`
      CREATE OR REPLACE FUNCTION update_product_detail_stock_qty()
          RETURNS TRIGGER AS $$
          BEGIN
            UPDATE product_detail_stocks
            SET qty = qty + NEW.qty - OLD.qty, locked_qty = locked_qty + NEW.locked_qty - OLD.locked_qty,
            uom_value = uom_value + NEW.uom_value - OLD.uom_value, locked_uom_value = locked_uom_value + NEW.locked_uom_value - OLD.locked_uom_value
            WHERE product_detail_id = NEW.product_detail_id;
            RETURN NEW;
          END;
          $$ LANGUAGE plpgsql;
    `);
            await trx.query(`
          CREATE TRIGGER inventories_trigger
          AFTER UPDATE or INSERT ON inventories
          FOR EACH ROW
          EXECUTE FUNCTION update_product_detail_stock_qty();

      
      `);
        });
    }
    async down(queryRunner) {
        await queryRunner.query('DROP TRIGGER inventories_trigger ON inventories');
        await queryRunner.query('DROP FUNCTION IF EXISTS update_product_detail_stock_qty()');
    }
}
exports.UpdateProductDetailStockQty1687413760311 = UpdateProductDetailStockQty1687413760311;
//# sourceMappingURL=1687413760311-UpdateProductDetailStockQty.js.map