const sequelize = global.database
const { Op } = require('sequelize');
const {
    UserModel,
    BusinessModel
} = global.config

module.exports = async (ctx) => {
    let data = ctx.update.callback_query.data;
    data = JSON.parse(data)
    let user = await UserModel.findByPk(ctx.update.callback_query.from.id)
    if (data.type == 'business_invite_accept') {
      if(user.business.id == 0){
        let business = await BusinessModel.findOne({
          where: {
            owner: data.invitor
          }
        })
        user.business = {
            id: data.invitor,
            checks: 0,
            percent: 10
        }
        business.users = sequelize.fn('array_append', sequelize.col('users'), ctx.update.callback_query.from.id);
        business.save()
        user.save()
        await ctx.telegram.answerCbQuery(ctx.callbackQuery.id, `✔️ Приглашение принято.`, {show_alert: true})
      }
      await ctx.telegram.answerCbQuery(ctx.callbackQuery.id, `ℹ️ Вы уже в организации.`, {show_alert: true})
    } 
    if (data.type == 'business_invite_refuse'){
        await ctx.telegram.answerCbQuery(ctx.callbackQuery.id, `❌ Приглашение отклонено.`, {show_alert: true})
    }
}