UNPKG

@grouparoo/core

Version:
113 lines (112 loc) 4.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SyncCLI = void 0; const cli_1 = require("../modules/cli"); const __1 = require(".."); const actionhero_1 = require("actionhero"); const sequelize_1 = require("sequelize"); class SyncCLI extends actionhero_1.CLI { constructor() { super(); this.name = "sync <recordProperty>"; this.description = "Sync (import & export) a GrouparooRecord. You can provide a GrouparooRecord id or a unique GrouparooRecord Property"; this.inputs = { property: { description: "Choose the name of the Property to find the GrouparooRecord by (i.e.: email_address)", letter: "p", }, "no-export": { description: "Skip exporting the record", letter: "n", flag: true, }, }; this.example = `grouparoo sync person@example.com --property email`; this.preInitialize = () => { cli_1.GrouparooCLI.setGrouparooRunMode(this); cli_1.GrouparooCLI.setNextDevelopmentMode(); }; cli_1.GrouparooCLI.timestampOption(this); cli_1.GrouparooCLI.jsonOption(this); } async run({ params, }) { const [recordProperty] = params._arguments || []; if (recordProperty) params.recordProperty = recordProperty; if (!params.recordProperty) { return cli_1.GrouparooCLI.logger.fatal("No GrouparooRecord Property provided"); } if (typeof params.property === "boolean") { return cli_1.GrouparooCLI.logger.fatal("Provide the property's name"); } if (!params.json) { cli_1.GrouparooCLI.logCLI(this.name.replace(" <recordProperty>", params.recordProperty ? " " + params.recordProperty : "")); } const uniqueProperties = (await __1.Property.findAll({ where: { unique: true } })).filter((p) => (params.property ? p.key === params.property : true)); const recordProperties = await __1.RecordProperty.findAll({ where: { rawValue: params.recordProperty, propertyId: { [sequelize_1.Op.in]: uniqueProperties.map((p) => p.id) }, }, }); const wheres = []; if (recordProperties.length > 1) { return cli_1.GrouparooCLI.logger.fatal(`More than one GrouparooRecord found with GrouparooRecord Property "${params.recordProperty}". Please provide [property]`); } else if (recordProperties.length === 1) { wheres.push({ id: recordProperties[0].recordId }); } else { wheres.push({ id: params.recordProperty }); } const record = await __1.GrouparooRecord.findOne({ where: { [sequelize_1.Op.or]: wheres }, }); if (!record) { return cli_1.GrouparooCLI.logger.fatal(params.property ? `Cannot find GrouparooRecord where GrouparooRecord Property "${params.property}"="${params.recordProperty}"` : `Cannot find GrouparooRecord with id or unique GrouparooRecord Property "${params.recordProperty}"`); } const { properties, groups, exports } = await record.snapshot(params.export); if (params.json) { cli_1.GrouparooCLI.logger.log(JSON.stringify({ properties, groups, exports })); } else { const propertyStatus = {}; const groupStatus = {}; const exportStatus = {}; for (const k in properties) propertyStatus[k] = [properties[k].values.join(", ")]; for (const i in groups) groupStatus[groups[i].name] = []; for (const i in exports) { exportStatus[exports[i].destinationName + ": Properties"] = [ Object.keys(exports[i].newRecordProperties) .map((k) => `${k}: ${exports[i].newRecordProperties[k]}`) .join(", "), ]; exportStatus[exports[i].destinationName + ": Groups"] = [ exports[i].newGroups.join(", "), ]; } cli_1.GrouparooCLI.logger.status(`GrouparooRecord`, [ { header: "Info", status: { id: [record.id], createdAt: [record.createdAt.toISOString()], state: [record.state], }, }, { header: "GrouparooRecord Properties", status: propertyStatus }, { header: "Groups", status: groupStatus }, { header: params.export ? "Exports" : "Next Exports", status: exportStatus, }, ], record.id); } return true; } } exports.SyncCLI = SyncCLI;