@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
75 lines (74 loc) • 3.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const BaseController_1 = __importDefault(require("../controllers/BaseController"));
const mongodb_1 = require("../plugins/mongodb");
const ServiceAccountService_1 = require("../services/ServiceAccountService");
const WorkspaceService_1 = require("../services/WorkspaceService");
class ServiceAccountController extends BaseController_1.default {
constructor() {
super(new ServiceAccountService_1.ServiceAccountService());
}
read(queryParams) {
return super.read();
}
create(body, queryParams) {
return super.create(body);
}
update(body, queryParams) {
return super.update(body);
}
delete(queryParams) {
return super.delete();
}
async joinWorkspace(data) {
const { userId, workspace: workspaceSlug } = data;
const result = { status: 1, messages: [], data: {} };
// console.log("{ userId, workspace } :>> ", { userId, workspace });
try {
if (!userId)
throw new Error(`"userId" is required.`);
if (!workspaceSlug)
throw new Error(`"workspaceSlug" is required.`);
// console.log("===========");
// console.log("userId, workspaceSlug :>> ", userId, workspaceSlug);
const workspaceSvc = new WorkspaceService_1.WorkspaceService();
const workspace = await workspaceSvc.findOne({ slug: workspaceSlug });
if (!workspace)
throw new Error(`Workspace "${workspaceSlug}" not found.`);
// console.log("workspace :>> ", workspace);
const wsId = mongodb_1.MongoDB.toString(workspace._id);
const user = await this.service.findOne({ id: userId });
// console.log("user :>> ", user);
// console.log("wsId :>> ", wsId);
// validations
if (!user)
throw new Error(`User not found.`);
if (!workspace.public)
throw new Error(`This workspace is private, you need to ask the administrator to add you in first.`);
let updatedUser = [user];
const isUserJoinedThisWorkspace = (user.workspaces || []).includes(wsId);
// console.log("isUserJoinedThisWorkspace :>> ", isUserJoinedThisWorkspace);
const isWorkspaceActive = typeof user.activeWorkspace !== "undefined" && user.activeWorkspace === wsId;
// console.log("isWorkspaceActive :>> ", isWorkspaceActive);
// console.log("user.workspaces :>> ", user.workspaces);
if (!isUserJoinedThisWorkspace) {
updatedUser = await this.service.update({ _id: userId }, { $push: { workspaces: workspace._id } }, { raw: true });
}
// console.log("[1] updatedUser :>> ", updatedUser[0]);
// make this workspace active
if (!isWorkspaceActive)
updatedUser = await this.service.update({ _id: userId }, { activeWorkspace: wsId });
// console.log("[2] updatedUser :>> ", updatedUser[0]);
result.data = updatedUser[0];
}
catch (e) {
result.messages.push(e.message);
result.status = 0;
}
return result;
}
}
exports.default = ServiceAccountController;