import { Controller } from 'egg';
import { RegionModel } from '@db/Region';
import { STATUS } from '@consts';
import { checkWhiteList } from 'app/pubUtils/sysUtil';

export default class UpdateController extends Controller {
  public async getversion() {
    const { ctx } = this;
    const { version } = ctx.request.body;
    ctx.body = await ctx.service.update.getVersion(version);
  }

  public async getUpdateUrl() {
    const { ctx } = this;
    const { addressType } = ctx.request.body;

    const env = this.app.config.realEnv;
    const curRegion = await RegionModel.findRegionByEnv(env);
    const { curVersion, whitelistVersion, updateResUrl, addressType: originAddressType } = curRegion;

    if(originAddressType != addressType) {
      return ctx.body = ctx.service.utils.resResult(STATUS.ADDRESS_ERR);
    }
    let isWhiteList = await checkWhiteList(ctx.app.config.realEnv, ctx.clientIp, ctx.uid);

    ctx.body = await ctx.service.update.getUpdateUrl(env, isWhiteList? whitelistVersion: curVersion, updateResUrl);
  }
}
