@eyeseetea/d2-logger 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -23
- package/data/repositories/ProgramD2Repository.js +7 -9
- package/data/repositories/ProgramLoggerD2Repository.js +8 -10
- package/domain/entities/LoggerConfig.d.ts +5 -4
- package/domain/repositories/LoggerRepository.d.ts +3 -1
- package/domain/usecases/LogMessageUseCase.d.ts +3 -1
- package/domain/usecases/LogMessageUseCase.js +2 -2
- package/index.js +2 -1
- package/package.json +2 -2
- package/scripts/commands/programLogger.js +1 -1
package/README.md
CHANGED
|
@@ -19,28 +19,21 @@ There are two types of logger output:
|
|
|
19
19
|
|
|
20
20
|
await logger.init({
|
|
21
21
|
type: "program",
|
|
22
|
-
debug:
|
|
23
|
-
baseUrl:
|
|
24
|
-
auth:
|
|
25
|
-
organisationUnitId:
|
|
26
|
-
programId:
|
|
27
|
-
|
|
28
|
-
messageId:
|
|
29
|
-
messageTypeId:
|
|
22
|
+
debug: true,
|
|
23
|
+
baseUrl: "https://play.dhis2.org/40.2.2",
|
|
24
|
+
auth: "admin:district",
|
|
25
|
+
organisationUnitId: "H8RixfF8ugH",
|
|
26
|
+
programId: "zARxYmOD18Z",
|
|
27
|
+
dataElements: {
|
|
28
|
+
messageId: "BjUzF5E4eR8",
|
|
29
|
+
messageTypeId: "NpS5LoLuhgS",
|
|
30
30
|
},
|
|
31
31
|
});
|
|
32
|
-
|
|
33
|
-
logger.debug("This is a Debug message");
|
|
34
|
-
logger.info("This is an Info message");
|
|
35
|
-
logger.success("This is a Success message");
|
|
36
|
-
logger.warn("This is a Warn message");
|
|
37
|
-
logger.error("This is an Error message");
|
|
38
32
|
```
|
|
39
33
|
|
|
40
34
|
Notice:
|
|
41
35
|
|
|
42
36
|
- Please note that `auth` is not mandatory if it's used in the DHIS2 app instead of in a script.
|
|
43
|
-
- Please note that if the `organisationUnitId` is not added to the configuration, then the logs will be registered in the DHIS2 program in the global organization unit.
|
|
44
37
|
- If `debug` is `true`, then in addition to registering the logs in the DHIS2 program, they will also be displayed on the console.
|
|
45
38
|
|
|
46
39
|
2. Displaying the logs only in the console:
|
|
@@ -53,17 +46,22 @@ There are two types of logger output:
|
|
|
53
46
|
logger.init({
|
|
54
47
|
type: "console",
|
|
55
48
|
});
|
|
56
|
-
|
|
57
|
-
logger.debug("This is a Debug message");
|
|
58
|
-
logger.info("This is an Info message");
|
|
59
|
-
logger.success("This is a Success message");
|
|
60
|
-
logger.warn("This is a Warn message");
|
|
61
|
-
logger.error("This is an Error message");
|
|
62
49
|
```
|
|
63
50
|
|
|
51
|
+
To log messages:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
logger.debug("This is a Debug message");
|
|
55
|
+
logger.info("This is an Info message");
|
|
56
|
+
logger.success("This is a Success message");
|
|
57
|
+
logger.warn("This is a Warn message");
|
|
58
|
+
logger.error("This is an Error message");
|
|
59
|
+
```
|
|
60
|
+
|
|
64
61
|
## Development
|
|
65
62
|
|
|
66
63
|
```bash
|
|
64
|
+
$ nvm use # Selects node in .nvmrc
|
|
67
65
|
$ yarn install
|
|
68
66
|
$ yarn build
|
|
69
67
|
$ cd build
|
|
@@ -90,7 +88,8 @@ $ yarn code-quality
|
|
|
90
88
|
|
|
91
89
|
## Publish
|
|
92
90
|
|
|
91
|
+
First, change in package.json the new version and then run:
|
|
92
|
+
|
|
93
93
|
```bash
|
|
94
|
-
$ yarn
|
|
95
|
-
$ yarn release [--tag beta] [--patch | --minor | --major]
|
|
94
|
+
$ yarn release
|
|
96
95
|
```
|
|
@@ -8,20 +8,18 @@ var ProgramD2Repository = /** @class */ (function () {
|
|
|
8
8
|
function ProgramD2Repository() {
|
|
9
9
|
}
|
|
10
10
|
ProgramD2Repository.prototype.checkConfig = function (config) {
|
|
11
|
-
var baseUrl = config.baseUrl, auth = config.auth, programId = config.programId,
|
|
12
|
-
var d2Api = auth
|
|
13
|
-
|
|
14
|
-
: new d2_api_1.D2Api({ baseUrl: baseUrl });
|
|
15
|
-
return this.checkProgramIsOk(d2Api, programId, loggerDataElements).flatMap(function (programIsOk) {
|
|
11
|
+
var baseUrl = config.baseUrl, auth = config.auth, programId = config.programId, dataElements = config.dataElements;
|
|
12
|
+
var d2Api = new d2_api_1.D2Api({ baseUrl: baseUrl, auth: auth });
|
|
13
|
+
return this.checkProgramIsOk(d2Api, programId, dataElements).flatMap(function (programIsOk) {
|
|
16
14
|
if (programIsOk) {
|
|
17
15
|
return Future_1.Future.success(programIsOk);
|
|
18
16
|
}
|
|
19
17
|
else {
|
|
20
|
-
return Future_1.Future.error(new Error("Logger not initialized properly. Please check configuration of program ".concat(programId, ", message data element ").concat(
|
|
18
|
+
return Future_1.Future.error(new Error("Logger not initialized properly. Please check configuration of program ".concat(programId, ", message data element ").concat(dataElements.messageId, " and message type data element ").concat(dataElements.messageTypeId, ".")));
|
|
21
19
|
}
|
|
22
20
|
});
|
|
23
21
|
};
|
|
24
|
-
ProgramD2Repository.prototype.checkProgramIsOk = function (api, programId,
|
|
22
|
+
ProgramD2Repository.prototype.checkProgramIsOk = function (api, programId, dataElements) {
|
|
25
23
|
return (0, api_futures_1.apiToFuture)(api.models.programs.get({
|
|
26
24
|
fields: programFields,
|
|
27
25
|
filter: { id: { eq: programId } },
|
|
@@ -31,11 +29,11 @@ var ProgramD2Repository = /** @class */ (function () {
|
|
|
31
29
|
var programStageDataElements = response.objects[0].programStages[0].programStageDataElements;
|
|
32
30
|
var messageDataElement = (_b = programStageDataElements.find(function (_a) {
|
|
33
31
|
var dataElement = _a.dataElement;
|
|
34
|
-
return dataElement.id === (
|
|
32
|
+
return dataElement.id === (dataElements === null || dataElements === void 0 ? void 0 : dataElements.messageId);
|
|
35
33
|
})) === null || _b === void 0 ? void 0 : _b.dataElement;
|
|
36
34
|
var messageTypeDataElement = (_c = programStageDataElements.find(function (_a) {
|
|
37
35
|
var dataElement = _a.dataElement;
|
|
38
|
-
return dataElement.id === (
|
|
36
|
+
return dataElement.id === (dataElements === null || dataElements === void 0 ? void 0 : dataElements.messageTypeId);
|
|
39
37
|
})) === null || _c === void 0 ? void 0 : _c.dataElement;
|
|
40
38
|
var isMessageDataElementOk = (messageDataElement === null || messageDataElement === void 0 ? void 0 : messageDataElement.valueType) === "TEXT" ||
|
|
41
39
|
(messageDataElement === null || messageDataElement === void 0 ? void 0 : messageDataElement.valueType) === "LONG_TEXT";
|
|
@@ -4,19 +4,16 @@ exports.ProgramLoggerD2Repository = void 0;
|
|
|
4
4
|
var d2_api_1 = require("../../types/d2-api");
|
|
5
5
|
var api_futures_1 = require("../api-futures");
|
|
6
6
|
var Future_1 = require("../../domain/entities/generic/Future");
|
|
7
|
-
var GLOBAL_ORGANISATION_UNIT_ID = "H8RixfF8ugH";
|
|
8
7
|
var IMPORT_STRATEGY_CREATE = "CREATE";
|
|
9
8
|
var TRACKER_IMPORT_JOB = "TRACKER_IMPORT_JOB";
|
|
10
9
|
var EVENT_PROGRAM_STATUS = "COMPLETED";
|
|
11
10
|
var ProgramLoggerD2Repository = /** @class */ (function () {
|
|
12
11
|
function ProgramLoggerD2Repository(config) {
|
|
13
|
-
var baseUrl = config.baseUrl, auth = config.auth, programId = config.programId,
|
|
14
|
-
this.api = auth
|
|
15
|
-
? new d2_api_1.D2Api({ baseUrl: baseUrl, auth: auth })
|
|
16
|
-
: new d2_api_1.D2Api({ baseUrl: baseUrl });
|
|
12
|
+
var baseUrl = config.baseUrl, auth = config.auth, programId = config.programId, dataElements = config.dataElements, organisationUnitId = config.organisationUnitId;
|
|
13
|
+
this.api = new d2_api_1.D2Api({ baseUrl: baseUrl, auth: auth });
|
|
17
14
|
this.programId = programId;
|
|
18
|
-
this.messageId =
|
|
19
|
-
this.messageTypeId =
|
|
15
|
+
this.messageId = dataElements.messageId;
|
|
16
|
+
this.messageTypeId = dataElements.messageTypeId;
|
|
20
17
|
this.organisationUnitId = organisationUnitId;
|
|
21
18
|
}
|
|
22
19
|
ProgramLoggerD2Repository.prototype.log = function (log) {
|
|
@@ -39,9 +36,10 @@ var ProgramLoggerD2Repository = /** @class */ (function () {
|
|
|
39
36
|
fields: programFields,
|
|
40
37
|
filter: { id: { eq: this.programId } },
|
|
41
38
|
})).flatMap(function (response) {
|
|
42
|
-
var _a
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
var _a;
|
|
40
|
+
var programStage = (_a = response.objects[0]) === null || _a === void 0 ? void 0 : _a.programStages[0];
|
|
41
|
+
if (programStage) {
|
|
42
|
+
return Future_1.Future.success(programStage);
|
|
45
43
|
}
|
|
46
44
|
else {
|
|
47
45
|
return Future_1.Future.error(new Error("Program stage of program with id ".concat(_this.programId, " not found")));
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Maybe } from "../../utils/ts-utils";
|
|
1
2
|
import { Id } from "./Base";
|
|
2
3
|
interface LoggerConfigBase {
|
|
3
4
|
debug?: boolean;
|
|
@@ -9,16 +10,16 @@ export interface ConsoleLoggerConfig extends LoggerConfigBase {
|
|
|
9
10
|
export interface ProgramLoggerConfig extends LoggerConfigBase {
|
|
10
11
|
type: "program";
|
|
11
12
|
baseUrl: string;
|
|
12
|
-
auth
|
|
13
|
+
auth: Maybe<Auth>;
|
|
13
14
|
programId: Id;
|
|
14
|
-
|
|
15
|
-
organisationUnitId
|
|
15
|
+
dataElements: DataElements;
|
|
16
|
+
organisationUnitId: Id;
|
|
16
17
|
}
|
|
17
18
|
type Auth = {
|
|
18
19
|
username: string;
|
|
19
20
|
password: string;
|
|
20
21
|
};
|
|
21
|
-
export type
|
|
22
|
+
export type DataElements = {
|
|
22
23
|
messageId: Id;
|
|
23
24
|
messageTypeId: Id;
|
|
24
25
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { FutureData } from "../../data/api-futures";
|
|
2
2
|
import { Log } from "../../domain/entities/Log";
|
|
3
3
|
export interface LoggerRepository {
|
|
4
|
-
log(log: Log,
|
|
4
|
+
log(log: Log, options?: {
|
|
5
|
+
isDebug: boolean;
|
|
6
|
+
}): FutureData<void>;
|
|
5
7
|
}
|
|
@@ -4,5 +4,7 @@ import { LoggerRepository } from "../../domain/repositories/LoggerRepository";
|
|
|
4
4
|
export declare class LogMessageUseCase {
|
|
5
5
|
private loggerRepository;
|
|
6
6
|
constructor(loggerRepository: LoggerRepository);
|
|
7
|
-
execute(log: Log,
|
|
7
|
+
execute(log: Log, options?: {
|
|
8
|
+
isDebug?: boolean;
|
|
9
|
+
}): FutureData<void>;
|
|
8
10
|
}
|
|
@@ -5,8 +5,8 @@ var LogMessageUseCase = /** @class */ (function () {
|
|
|
5
5
|
function LogMessageUseCase(loggerRepository) {
|
|
6
6
|
this.loggerRepository = loggerRepository;
|
|
7
7
|
}
|
|
8
|
-
LogMessageUseCase.prototype.execute = function (log,
|
|
9
|
-
if (isDebug) {
|
|
8
|
+
LogMessageUseCase.prototype.execute = function (log, options) {
|
|
9
|
+
if (options === null || options === void 0 ? void 0 : options.isDebug) {
|
|
10
10
|
// eslint-disable-next-line no-console
|
|
11
11
|
console.log(log);
|
|
12
12
|
}
|
package/index.js
CHANGED
|
@@ -85,8 +85,9 @@ var Logger = /** @class */ (function () {
|
|
|
85
85
|
};
|
|
86
86
|
Logger.prototype.log = function (message, messageType) {
|
|
87
87
|
if (this.loggerRepository) {
|
|
88
|
+
var options = { isDebug: this.isDebug };
|
|
88
89
|
return new LogMessageUseCase_1.LogMessageUseCase(this.loggerRepository)
|
|
89
|
-
.execute({ message: message, messageType: messageType },
|
|
90
|
+
.execute({ message: message, messageType: messageType }, options)
|
|
90
91
|
.toPromise();
|
|
91
92
|
}
|
|
92
93
|
else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eyeseetea/d2-logger",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "DHIS2 library that allows a certain application to register events in a DHIS2 program",
|
|
5
5
|
"author": "EyeSeeTea team <info@eyeseetea.com>",
|
|
6
6
|
"license": "GPL-3.0",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"code-quality": "yarn lint && yarn prettify && echo disabled yarn test",
|
|
19
19
|
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
|
|
20
20
|
"prettify": "prettier \"{.,src}/**/*.{js,jsx,ts,tsx,json}\" --write",
|
|
21
|
-
"
|
|
21
|
+
"prerelease": "yarn build",
|
|
22
22
|
"release": "bash scripts/publish.sh",
|
|
23
23
|
"test": "vitest",
|
|
24
24
|
"script-program-example": "npx ts-node src/scripts/index.ts programLogger log",
|