package com.red.circle.other.app.command.room;

import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfileManager;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.inner.asserts.RoomErrorCode;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 清除房间密码
 *
 * @author houyong on 2020/12/16
 */
@Component
@RequiredArgsConstructor
public class UnlockedRoomCmdExe {

  private final RoomProfileManagerService roomProfileManagerService;

  public void execute(AppRoomIdCmd cmd) {
    RoomProfileManager manager = roomProfileManagerService.getById(cmd.getRoomId());
    // 房间资料缺失
    ResponseAssert.notNull(RoomErrorCode.ROOM_INFO_MISSING, manager);
    // 权限不足
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
        Objects.equals(cmd.requiredReqUserId(), manager.getUserId()));
    roomProfileManagerService.updatePassword(cmd.getRoomId(), "");
  }

}
