"use strict";

const { RealEstate } = require("../models");

module.exports = {
  async up(queryInterface, Sequelize) {
    //Reading initial data from RealEstate table in db
    const realEstateInitialData = await RealEstate.findAll();
    //Extruding data for table PriceHistory
    const priceHistoryInitialData = realEstateInitialData.map(realEstate => {
      //Null values canot be recognized by ignore duplicates in sequalize
      //Value price = 0 indicates 'cijena na upit'
      const priceTmp =
        realEstate.dataValues.price === null ? 0 : realEstate.dataValues.price;

      return {
        realEstateId: realEstate.dataValues.id,
        price: priceTmp,
        createdAt: realEstate.dataValues.createdAt,
        updatedAt: realEstate.dataValues.updatedAt
      };
    });

    return queryInterface.bulkInsert(
      "PriceHistory",
      priceHistoryInitialData,
      {}
    );
  },

  async down(queryInterface, Sequelize) {
    return queryInterface.bulkDelete("PriceHistory", null, {});
  }
};
