UNPKG

@grouparoo/core

Version:
255 lines (254 loc) 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupDestroy = exports.GroupListDestinations = exports.GroupCountPotentialMembers = exports.GroupCountComponentMembers = exports.GroupView = exports.GroupRun = exports.GroupEdit = exports.GroupCreate = exports.GroupsRuleOptions = exports.GroupsList = void 0; const authenticatedAction_1 = require("../classes/actions/authenticatedAction"); const Group_1 = require("../models/Group"); const ruleOpsDictionary_1 = require("../modules/ruleOpsDictionary"); const topLevelGroupRules_1 = require("../modules/topLevelGroupRules"); const GroupMember_1 = require("../models/GroupMember"); const configWriter_1 = require("../modules/configWriter"); const apiData_1 = require("../modules/apiData"); class GroupsList extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "groups:list"; this.description = "list all the groups"; this.outputExample = {}; this.permission = { topic: "group", mode: "read" }; this.inputs = { limit: { required: true, default: 100, formatter: apiData_1.APIData.ensureNumber }, offset: { required: true, default: 0, formatter: apiData_1.APIData.ensureNumber }, state: { required: false }, modelId: { required: false }, order: { required: false, formatter: apiData_1.APIData.ensureArray, default: [ ["name", "desc"], ["createdAt", "desc"], ], }, }; } async runWithinTransaction({ params }) { const where = {}; if (params.state) where["state"] = params.state; if (params.modelId) where["modelId"] = params.modelId; const groups = await Group_1.Group.scope(null).findAll({ where, limit: params.limit, offset: params.offset, order: params.order, }); const total = await Group_1.Group.scope(null).count({ where }); return { total, groups: await Promise.all(groups.map((g) => g.apiData())), }; } } exports.GroupsList = GroupsList; class GroupsRuleOptions extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "groups:ruleOptions"; this.description = "send the options about groups to the UI"; this.outputExample = {}; this.permission = { topic: "group", mode: "read" }; } async runWithinTransaction() { return { ruleLimit: Group_1.GROUP_RULE_LIMIT, ops: ruleOpsDictionary_1.PropertyOpsDictionary, topLevelGroupRules: topLevelGroupRules_1.TopLevelGroupRules, }; } } exports.GroupsRuleOptions = GroupsRuleOptions; class GroupCreate extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:create"; this.description = "create a group"; this.outputExample = {}; this.permission = { topic: "group", mode: "write" }; this.inputs = { name: { required: true }, modelId: { required: true }, matchType: { required: true, default: "all" }, rules: { required: false, formatter: apiData_1.APIData.ensureArray }, state: { required: false }, }; } async runWithinTransaction({ params }) { const group = await Group_1.Group.create({ name: params.name, modelId: params.modelId, matchType: params.matchType, state: params.state, }); if (params.rules) await group.setRules(params.rules); const responseGroup = await group.apiData(); responseGroup.rules = group.toConvenientRules(await group.getRules()); await configWriter_1.ConfigWriter.run(); return { group: responseGroup }; } } exports.GroupCreate = GroupCreate; class GroupEdit extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:edit"; this.description = "edit a group"; this.outputExample = {}; this.permission = { topic: "group", mode: "write" }; this.inputs = { id: { required: true }, name: { required: false }, matchType: { required: false }, rules: { required: false, formatter: apiData_1.APIData.ensureArray }, }; } async runWithinTransaction({ params }) { const group = await Group_1.Group.findById(params.id); await group.update(params); if (params.rules) await group.setRules(params.rules); const responseGroup = await group.apiData(); responseGroup.rules = group.toConvenientRules(await group.getRules()); await configWriter_1.ConfigWriter.run(); return { group: responseGroup }; } } exports.GroupEdit = GroupEdit; class GroupRun extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:run"; this.description = "recalculate the members for a group"; this.outputExample = {}; this.permission = { topic: "group", mode: "write" }; this.inputs = { id: { required: true }, }; } async runWithinTransaction({ params }) { const group = await Group_1.Group.findById(params.id); await group.run(); return { success: true }; } } exports.GroupRun = GroupRun; class GroupView extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:view"; this.description = "view a group and members"; this.outputExample = {}; this.permission = { topic: "group", mode: "read" }; this.inputs = { id: { required: true }, }; } async runWithinTransaction({ params }) { const group = await Group_1.Group.findById(params.id); const responseGroup = await group.apiData(); responseGroup.rules = group.toConvenientRules(await group.getRules()); return { group: responseGroup }; } } exports.GroupView = GroupView; class GroupCountComponentMembers extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:countComponentMembers"; this.description = "return the counts of records which exist due to a certain rule"; this.outputExample = {}; this.permission = { topic: "group", mode: "read" }; this.inputs = { id: { required: true }, rules: { required: false, formatter: apiData_1.APIData.ensureArray }, }; } async runWithinTransaction({ params, }) { const group = await Group_1.Group.findById(params.id); const { componentCounts, funnelCounts } = await group.countComponentMembersFromRules(group.fromConvenientRules(params.rules)); return { componentCounts, funnelCounts }; } } exports.GroupCountComponentMembers = GroupCountComponentMembers; class GroupCountPotentialMembers extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:countPotentialMembers"; this.description = "return the count of records that would match these rules"; this.outputExample = {}; this.permission = { topic: "group", mode: "read" }; this.inputs = { id: { required: true }, rules: { required: false, formatter: apiData_1.APIData.ensureArray }, }; } async runWithinTransaction({ params, }) { const group = await Group_1.Group.findById(params.id); const count = await group.countPotentialMembers(group.fromConvenientRules(params.rules)); return { count }; } } exports.GroupCountPotentialMembers = GroupCountPotentialMembers; class GroupListDestinations extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:listDestinations"; this.description = "list the destinations interested in this group"; this.outputExample = {}; this.permission = { topic: "group", mode: "read" }; this.inputs = { id: { required: true }, }; } async runWithinTransaction({ params, }) { const group = await Group_1.Group.findById(params.id); const destinations = await group.$get("destinations"); return { total: destinations.length, destinations: await Promise.all(destinations.map((d) => d.apiData())), }; } } exports.GroupListDestinations = GroupListDestinations; class GroupDestroy extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "group:destroy"; this.description = "destroy a group"; this.outputExample = {}; this.permission = { topic: "group", mode: "write" }; this.inputs = { id: { required: true }, force: { required: true, default: false, formatter: apiData_1.APIData.ensureBoolean, }, }; } async runWithinTransaction({ params }) { const group = await Group_1.Group.findById(params.id); await Group_1.Group.ensureNotInUse(group); if (params.force === true) { await GroupMember_1.GroupMember.destroy({ where: { groupId: group.id } }); await group.destroy(); // other related models are handled by hooks } else { // group:destroy will be eventually enqueued by the `destroy` system task await group.update({ state: "deleted" }); } await configWriter_1.ConfigWriter.run(); return { success: true }; } } exports.GroupDestroy = GroupDestroy;