@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
177 lines • 8.88 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const console_1 = require("console");
const fs = require("fs");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const path = require("path");
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("cases")
.alias("cs")
.option("-m, --mode [list|create|update|delete|template|info]", "list | create | update | delete | template | info", "list")
.option("-f, --file <file>", ".mdsp.json file with case definition", "case.mdsp.json")
.option("-n, --case <case>", "the case name")
.option("-i, --handle <handle>", "the case handle")
.option("-o, --overwrite", "overwrite template file if it already exists")
.option("-k, --passkey <passkey>", "passkey")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color("list, create or delete cases *"))
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkRequiredParamaters(options);
const sdk = (0, command_utils_1.getSdk)(options);
color = (0, command_utils_1.adjustColor)(color, options);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
switch (options.mode) {
case "list":
yield listCases(sdk, options);
break;
case "template":
createTemplate(options);
console.log("Edit the file before submitting it to Siemens Insights Hub.");
break;
case "delete":
yield deleteCase(options, sdk);
break;
case "update":
yield updateCase(options, sdk);
break;
case "create":
yield createCase(options, sdk);
break;
case "info":
yield caseInfo(options, sdk);
break;
default:
throw Error(`no such option: ${options.mode}`);
}
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp cases --mode list \t\t\t\tlist all cases`);
(0, console_1.log)(` mdsp cases --mode template --case <case> \tcreate a template file for <case>`);
(0, console_1.log)(` mdsp cases --mode create --file <case> \t\tcreate case `);
(0, console_1.log)(` mdsp cases --mode update --file <case> --handle <handle> \t update case `);
(0, console_1.log)(` mdsp cases --mode info --handle <handle> \case info for specified id`);
(0, console_1.log)(` mdsp cases --mode delete --handle <handle> \tdelete case with specified id`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function createCase(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.file);
const file = fs.readFileSync(filePath);
const case_ = JSON.parse(file.toString());
const result = yield sdk.GetCaseManagementClient().CreateCase(case_);
console.log(`creted case : ${color(result.handle)} message: ${color(result.message)}`);
});
}
function updateCase(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.file);
const file = fs.readFileSync(filePath);
const updatedCase = JSON.parse(file.toString());
const cm = sdk.GetCaseManagementClient();
const case_ = yield cm.GetCase(options.handle);
const result = yield cm.UpdateCase(options.handle, updatedCase, { ifMatch: case_.eTag });
console.log(`updated case : ${color(result.handle)} message: ${color(result.message)}`);
});
}
function createTemplate(options) {
const case_ = {
notifyAssignee: false,
dueDate: "2021-11-25",
title: "template case",
assignedTo: null,
priority: "LOW",
status: "OPEN",
type: "PLANNED",
description: "template",
attachments: [
{
name: "iris.csv",
assetId: "cb72dfd7400e4fc6a275f22e6751cce6",
path: "AA-019/2021-11-26T04:27:15.933Z",
},
],
associations: [
{
id: "cb72dfd7400e4fc6a275f22e6751cce6",
type: "ASSET",
},
],
};
(0, command_utils_1.verboseLog)(case_, options.verbose);
writeCaseToFile(options, case_);
}
function writeCaseToFile(options, case_) {
const fileName = options.file || `case.mdsp.json`;
const filePath = path.resolve(fileName);
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `);
fs.writeFileSync(fileName, JSON.stringify(case_, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp case --mode create --file ${fileName} \n\nto create the case`);
}
function deleteCase(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const id = options.handle;
const cm = sdk.GetCaseManagementClient();
const _case = yield cm.GetCase(id);
yield (0, utils_1.retry)(options.retry, () => sdk.GetCaseManagementClient().DeleteCase(id, { ifMatch: _case.eTag }));
console.log(`case with id ${color(id)} deleted.`);
});
}
function listCases(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const cases_ = (yield (0, utils_1.retry)(options.retry, () => sdk.GetCaseManagementClient().GetCases()));
// console.log(workorders);
console.log(`${color("handle")} title type dueDate ${color("status")} priority createdBy -> ${color("assignedTo")} `);
(_a = cases_.cases) === null || _a === void 0 ? void 0 : _a.forEach((case_) => {
console.log(`${color(case_.handle)} ${case_.title} ${case_.type} ${case_.dueDate} ${color(case_.status)} ${case_.priority} ${case_.createdBy} -> ${color(case_.assignedTo || "<none>")}`);
});
console.log(`${color(((_b = cases_.cases) === null || _b === void 0 ? void 0 : _b.length) || 0)} cases listed.\n`);
});
}
function caseInfo(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const case_ = yield (0, utils_1.retry)(options.retry, () => sdk.GetCaseManagementClient().GetCase(options.handle));
(0, command_utils_1.verboseLog)(JSON.stringify(case_, null, 2), options.verbose);
(0, command_utils_1.printObjectInfo)("Case", case_, options, ["handle", "id", "status", "type", "priority"], color);
});
}
function checkRequiredParamaters(options) {
options.mode === "create" &&
!options.file &&
(0, command_utils_1.errorLog)("you have to provide a file with case definition to create an workorder (see mdsp cases --help for more details)", true);
options.mode === "delete" &&
!options.handle &&
(0, command_utils_1.errorLog)("you have to provide the handle to delete (see mdsp cases --help for more details)", true);
options.mode === "delete" &&
!options.handle &&
(0, command_utils_1.errorLog)("you have to provide the handle to delete (see mdsp cases --help for more details)", true);
options.mode === "info" &&
!options.handle &&
(0, command_utils_1.errorLog)("you have to provide the handle (see mdsp cases --help for more details)", true);
}
//# sourceMappingURL=cases.js.map