@webiny/api-headless-cms-scheduler 0.0.0-unstable.06b2ede40f
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/LICENSE +21 -0
- package/README.md +15 -0
- package/constants.d.ts +11 -0
- package/constants.js +19 -0
- package/constants.js.map +1 -0
- package/context.d.ts +7 -0
- package/context.js +68 -0
- package/context.js.map +1 -0
- package/graphql/index.d.ts +3 -0
- package/graphql/index.js +204 -0
- package/graphql/index.js.map +1 -0
- package/graphql/schema.d.ts +217 -0
- package/graphql/schema.js +87 -0
- package/graphql/schema.js.map +1 -0
- package/handler/Handler.d.ts +23 -0
- package/handler/Handler.js +74 -0
- package/handler/Handler.js.map +1 -0
- package/handler/actions/PublishHandlerAction.d.ts +13 -0
- package/handler/actions/PublishHandlerAction.js +64 -0
- package/handler/actions/PublishHandlerAction.js.map +1 -0
- package/handler/actions/UnpublishHandlerAction.d.ts +13 -0
- package/handler/actions/UnpublishHandlerAction.js +53 -0
- package/handler/actions/UnpublishHandlerAction.js.map +1 -0
- package/handler/index.d.ts +7 -0
- package/handler/index.js +64 -0
- package/handler/index.js.map +1 -0
- package/handler/types.d.ts +5 -0
- package/handler/types.js +7 -0
- package/handler/types.js.map +1 -0
- package/hooks/index.d.ts +7 -0
- package/hooks/index.js +58 -0
- package/hooks/index.js.map +1 -0
- package/index.d.ts +10 -0
- package/index.js +29 -0
- package/index.js.map +1 -0
- package/manifest.d.ts +17 -0
- package/manifest.js +47 -0
- package/manifest.js.map +1 -0
- package/package.json +45 -0
- package/scheduler/ScheduleExecutor.d.ts +16 -0
- package/scheduler/ScheduleExecutor.js +55 -0
- package/scheduler/ScheduleExecutor.js.map +1 -0
- package/scheduler/ScheduleFetcher.d.ts +16 -0
- package/scheduler/ScheduleFetcher.js +51 -0
- package/scheduler/ScheduleFetcher.js.map +1 -0
- package/scheduler/ScheduleRecord.d.ts +31 -0
- package/scheduler/ScheduleRecord.js +57 -0
- package/scheduler/ScheduleRecord.js.map +1 -0
- package/scheduler/Scheduler.d.ts +14 -0
- package/scheduler/Scheduler.js +27 -0
- package/scheduler/Scheduler.js.map +1 -0
- package/scheduler/actions/PublishScheduleAction.d.ts +26 -0
- package/scheduler/actions/PublishScheduleAction.js +196 -0
- package/scheduler/actions/PublishScheduleAction.js.map +1 -0
- package/scheduler/actions/UnpublishScheduleAction.d.ts +26 -0
- package/scheduler/actions/UnpublishScheduleAction.js +194 -0
- package/scheduler/actions/UnpublishScheduleAction.js.map +1 -0
- package/scheduler/createScheduleRecordId.d.ts +2 -0
- package/scheduler/createScheduleRecordId.js +33 -0
- package/scheduler/createScheduleRecordId.js.map +1 -0
- package/scheduler/createScheduler.d.ts +12 -0
- package/scheduler/createScheduler.js +65 -0
- package/scheduler/createScheduler.js.map +1 -0
- package/scheduler/dates.d.ts +7 -0
- package/scheduler/dates.js +27 -0
- package/scheduler/dates.js.map +1 -0
- package/scheduler/model.d.ts +1 -0
- package/scheduler/model.js +88 -0
- package/scheduler/model.js.map +1 -0
- package/scheduler/types.d.ts +88 -0
- package/scheduler/types.js +19 -0
- package/scheduler/types.js.map +1 -0
- package/service/SchedulerService.d.ts +23 -0
- package/service/SchedulerService.js +151 -0
- package/service/SchedulerService.js.map +1 -0
- package/service/types.d.ts +19 -0
- package/service/types.js +7 -0
- package/service/types.js.map +1 -0
- package/types.d.ts +11 -0
- package/types.js +7 -0
- package/types.js.map +1 -0
- package/utils/dateInTheFuture.d.ts +6 -0
- package/utils/dateInTheFuture.js +19 -0
- package/utils/dateInTheFuture.js.map +1 -0
package/manifest.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.getManifest = void 0;
|
|
8
|
+
var _api = require("@webiny/api");
|
|
9
|
+
var _utils = require("@webiny/utils");
|
|
10
|
+
var _zod = _interopRequireDefault(require("zod"));
|
|
11
|
+
const schema = _zod.default.object({
|
|
12
|
+
scheduler: _zod.default.object({
|
|
13
|
+
lambdaArn: _zod.default.string(),
|
|
14
|
+
roleArn: _zod.default.string()
|
|
15
|
+
})
|
|
16
|
+
});
|
|
17
|
+
const getManifest = async params => {
|
|
18
|
+
try {
|
|
19
|
+
_api.ServiceDiscovery.setDocumentClient(params.client);
|
|
20
|
+
const manifest = await _api.ServiceDiscovery.load();
|
|
21
|
+
if (!manifest) {
|
|
22
|
+
return {
|
|
23
|
+
error: new Error("Manifest could not be loaded.")
|
|
24
|
+
};
|
|
25
|
+
} else if (!manifest.scheduler) {
|
|
26
|
+
return {
|
|
27
|
+
error: new Error("Scheduler not found in the Manifest.")
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const result = await schema.safeParseAsync(manifest);
|
|
31
|
+
if (!result.success) {
|
|
32
|
+
return {
|
|
33
|
+
error: (0, _utils.createZodError)(result.error)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
data: result.data.scheduler
|
|
38
|
+
};
|
|
39
|
+
} catch (ex) {
|
|
40
|
+
return {
|
|
41
|
+
error: ex
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.getManifest = getManifest;
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=manifest.js.map
|
package/manifest.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_api","require","_utils","_zod","_interopRequireDefault","schema","zod","object","scheduler","lambdaArn","string","roleArn","getManifest","params","ServiceDiscovery","setDocumentClient","client","manifest","load","error","Error","result","safeParseAsync","success","createZodError","data","ex","exports"],"sources":["manifest.ts"],"sourcesContent":["import { ServiceDiscovery } from \"@webiny/api\";\nimport type { DynamoDBDocument } from \"@webiny/aws-sdk/client-dynamodb\";\nimport { createZodError } from \"@webiny/utils\";\nimport zod from \"zod\";\n\nconst schema = zod.object({\n scheduler: zod.object({\n lambdaArn: zod.string(),\n roleArn: zod.string()\n })\n});\n\nexport interface IGetManifestErrorResult {\n error: Error;\n data?: never;\n}\n\nexport interface IGetManifestSuccessResult {\n data: {\n lambdaArn: string;\n roleArn: string;\n };\n error?: never;\n}\n\nexport type IGetManifestResult = IGetManifestSuccessResult | IGetManifestErrorResult;\n\nexport interface IGetManifestParams {\n client: DynamoDBDocument;\n}\n\nexport const getManifest = async (params: IGetManifestParams): Promise<IGetManifestResult> => {\n try {\n ServiceDiscovery.setDocumentClient(params.client);\n const manifest = await ServiceDiscovery.load();\n if (!manifest) {\n return {\n error: new Error(\"Manifest could not be loaded.\")\n };\n } else if (!manifest.scheduler) {\n return {\n error: new Error(\"Scheduler not found in the Manifest.\")\n };\n }\n\n const result = await schema.safeParseAsync(manifest);\n if (!result.success) {\n return {\n error: createZodError(result.error)\n };\n }\n\n return {\n data: result.data.scheduler\n };\n } catch (ex) {\n return {\n error: ex\n };\n }\n};\n"],"mappings":";;;;;;;AAAA,IAAAA,IAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,IAAA,GAAAC,sBAAA,CAAAH,OAAA;AAEA,MAAMI,MAAM,GAAGC,YAAG,CAACC,MAAM,CAAC;EACtBC,SAAS,EAAEF,YAAG,CAACC,MAAM,CAAC;IAClBE,SAAS,EAAEH,YAAG,CAACI,MAAM,CAAC,CAAC;IACvBC,OAAO,EAAEL,YAAG,CAACI,MAAM,CAAC;EACxB,CAAC;AACL,CAAC,CAAC;AAqBK,MAAME,WAAW,GAAG,MAAOC,MAA0B,IAAkC;EAC1F,IAAI;IACAC,qBAAgB,CAACC,iBAAiB,CAACF,MAAM,CAACG,MAAM,CAAC;IACjD,MAAMC,QAAQ,GAAG,MAAMH,qBAAgB,CAACI,IAAI,CAAC,CAAC;IAC9C,IAAI,CAACD,QAAQ,EAAE;MACX,OAAO;QACHE,KAAK,EAAE,IAAIC,KAAK,CAAC,+BAA+B;MACpD,CAAC;IACL,CAAC,MAAM,IAAI,CAACH,QAAQ,CAACT,SAAS,EAAE;MAC5B,OAAO;QACHW,KAAK,EAAE,IAAIC,KAAK,CAAC,sCAAsC;MAC3D,CAAC;IACL;IAEA,MAAMC,MAAM,GAAG,MAAMhB,MAAM,CAACiB,cAAc,CAACL,QAAQ,CAAC;IACpD,IAAI,CAACI,MAAM,CAACE,OAAO,EAAE;MACjB,OAAO;QACHJ,KAAK,EAAE,IAAAK,qBAAc,EAACH,MAAM,CAACF,KAAK;MACtC,CAAC;IACL;IAEA,OAAO;MACHM,IAAI,EAAEJ,MAAM,CAACI,IAAI,CAACjB;IACtB,CAAC;EACL,CAAC,CAAC,OAAOkB,EAAE,EAAE;IACT,OAAO;MACHP,KAAK,EAAEO;IACX,CAAC;EACL;AACJ,CAAC;AAACC,OAAA,CAAAf,WAAA,GAAAA,WAAA","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/api-headless-cms-scheduler",
|
|
3
|
+
"version": "0.0.0-unstable.06b2ede40f",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"description": "Schedule for Webiny Headless CMS",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"api-headless-cms-scheduler:base"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/webiny/webiny-js.git",
|
|
12
|
+
"directory": "packages/api-headless-cms-scheduler"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@webiny/api": "0.0.0-unstable.06b2ede40f",
|
|
17
|
+
"@webiny/api-headless-cms": "0.0.0-unstable.06b2ede40f",
|
|
18
|
+
"@webiny/aws-sdk": "0.0.0-unstable.06b2ede40f",
|
|
19
|
+
"@webiny/error": "0.0.0-unstable.06b2ede40f",
|
|
20
|
+
"@webiny/handler-graphql": "0.0.0-unstable.06b2ede40f",
|
|
21
|
+
"@webiny/utils": "0.0.0-unstable.06b2ede40f",
|
|
22
|
+
"zod": "3.23.8"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@webiny/api-i18n": "0.0.0-unstable.06b2ede40f",
|
|
26
|
+
"@webiny/api-security": "0.0.0-unstable.06b2ede40f",
|
|
27
|
+
"@webiny/api-tenancy": "0.0.0-unstable.06b2ede40f",
|
|
28
|
+
"@webiny/api-wcp": "0.0.0-unstable.06b2ede40f",
|
|
29
|
+
"@webiny/handler": "0.0.0-unstable.06b2ede40f",
|
|
30
|
+
"@webiny/handler-aws": "0.0.0-unstable.06b2ede40f",
|
|
31
|
+
"@webiny/plugins": "0.0.0-unstable.06b2ede40f",
|
|
32
|
+
"@webiny/project-utils": "0.0.0-unstable.06b2ede40f",
|
|
33
|
+
"aws-sdk-client-mock": "4.1.0",
|
|
34
|
+
"typescript": "5.3.3"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "node ../cli/bin.js run build",
|
|
38
|
+
"watch": "node ../cli/bin.js run watch"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"directory": "dist"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "06b2ede40fc2212a70eeafd74afd50b56fb0ce82"
|
|
45
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type IScheduleAction, type IScheduleExecutor, type IScheduleFetcher, type IScheduleRecord, type ISchedulerInput } from "./types.js";
|
|
2
|
+
import type { PublishScheduleActionCms } from "./actions/PublishScheduleAction.js";
|
|
3
|
+
import type { UnpublishScheduleActionCms } from "./actions/UnpublishScheduleAction.js";
|
|
4
|
+
export type ScheduleExecutorCms = UnpublishScheduleActionCms & PublishScheduleActionCms;
|
|
5
|
+
export interface IScheduleExecutorParams {
|
|
6
|
+
actions: IScheduleAction[];
|
|
7
|
+
fetcher: IScheduleFetcher;
|
|
8
|
+
}
|
|
9
|
+
export declare class ScheduleExecutor implements IScheduleExecutor {
|
|
10
|
+
private readonly actions;
|
|
11
|
+
private readonly fetcher;
|
|
12
|
+
constructor(params: IScheduleExecutorParams);
|
|
13
|
+
schedule(targetId: string, input: ISchedulerInput): Promise<IScheduleRecord>;
|
|
14
|
+
cancel(initialId: string): Promise<IScheduleRecord>;
|
|
15
|
+
private getAction;
|
|
16
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ScheduleExecutor = void 0;
|
|
7
|
+
var _createScheduleRecordId = require("./createScheduleRecordId.js");
|
|
8
|
+
var _error = require("@webiny/error");
|
|
9
|
+
class ScheduleExecutor {
|
|
10
|
+
constructor(params) {
|
|
11
|
+
this.actions = params.actions;
|
|
12
|
+
this.fetcher = params.fetcher;
|
|
13
|
+
}
|
|
14
|
+
async schedule(targetId, input) {
|
|
15
|
+
const scheduleRecordId = (0, _createScheduleRecordId.createScheduleRecordIdWithVersion)(targetId);
|
|
16
|
+
const original = await this.fetcher.getScheduled(targetId);
|
|
17
|
+
const action = this.getAction(input.type);
|
|
18
|
+
if (original) {
|
|
19
|
+
return action.reschedule(original, input);
|
|
20
|
+
}
|
|
21
|
+
return await action.schedule({
|
|
22
|
+
scheduleRecordId,
|
|
23
|
+
targetId,
|
|
24
|
+
input
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async cancel(initialId) {
|
|
28
|
+
const id = (0, _createScheduleRecordId.createScheduleRecordIdWithVersion)(initialId);
|
|
29
|
+
const original = await this.fetcher.getScheduled(id);
|
|
30
|
+
if (!original) {
|
|
31
|
+
throw new _error.WebinyError(`No scheduled record found for ID "${id}".`, "SCHEDULED_RECORD_NOT_FOUND", {
|
|
32
|
+
id
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const action = this.getAction(original.type);
|
|
36
|
+
await action.cancel(original.id);
|
|
37
|
+
return original;
|
|
38
|
+
}
|
|
39
|
+
getAction(type) {
|
|
40
|
+
const action = this.actions.find(action => {
|
|
41
|
+
return action.canHandle({
|
|
42
|
+
type
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
if (action) {
|
|
46
|
+
return action;
|
|
47
|
+
}
|
|
48
|
+
throw new _error.WebinyError(`No action found for input type "${type}".`, "NO_ACTION_FOUND", {
|
|
49
|
+
type
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ScheduleExecutor = ScheduleExecutor;
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=ScheduleExecutor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_createScheduleRecordId","require","_error","ScheduleExecutor","constructor","params","actions","fetcher","schedule","targetId","input","scheduleRecordId","createScheduleRecordIdWithVersion","original","getScheduled","action","getAction","type","reschedule","cancel","initialId","id","WebinyError","find","canHandle","exports"],"sources":["ScheduleExecutor.ts"],"sourcesContent":["import type { ScheduleType } from \"~/scheduler/types.js\";\nimport {\n type IScheduleAction,\n type IScheduleExecutor,\n type IScheduleFetcher,\n type IScheduleRecord,\n type ISchedulerInput\n} from \"~/scheduler/types.js\";\nimport { createScheduleRecordIdWithVersion } from \"~/scheduler/createScheduleRecordId.js\";\nimport type { PublishScheduleActionCms } from \"~/scheduler/actions/PublishScheduleAction.js\";\nimport type { UnpublishScheduleActionCms } from \"~/scheduler/actions/UnpublishScheduleAction.js\";\nimport { WebinyError } from \"@webiny/error\";\n\nexport type ScheduleExecutorCms = UnpublishScheduleActionCms & PublishScheduleActionCms;\n\nexport interface IScheduleExecutorParams {\n actions: IScheduleAction[];\n fetcher: IScheduleFetcher;\n}\n\nexport class ScheduleExecutor implements IScheduleExecutor {\n private readonly actions: IScheduleAction[];\n private readonly fetcher: Pick<IScheduleFetcher, \"getScheduled\">;\n\n constructor(params: IScheduleExecutorParams) {\n this.actions = params.actions;\n this.fetcher = params.fetcher;\n }\n\n public async schedule(targetId: string, input: ISchedulerInput): Promise<IScheduleRecord> {\n const scheduleRecordId = createScheduleRecordIdWithVersion(targetId);\n const original = await this.fetcher.getScheduled(targetId);\n\n const action = this.getAction(input.type);\n\n if (original) {\n return action.reschedule(original, input);\n }\n\n return await action.schedule({\n scheduleRecordId,\n targetId,\n input\n });\n }\n\n public async cancel(initialId: string): Promise<IScheduleRecord> {\n const id = createScheduleRecordIdWithVersion(initialId);\n const original = await this.fetcher.getScheduled(id);\n if (!original) {\n throw new WebinyError(\n `No scheduled record found for ID \"${id}\".`,\n \"SCHEDULED_RECORD_NOT_FOUND\",\n {\n id\n }\n );\n }\n\n const action = this.getAction(original.type);\n await action.cancel(original.id);\n return original;\n }\n\n private getAction(type: ScheduleType): IScheduleAction {\n const action = this.actions.find(action => {\n return action.canHandle({\n type\n });\n });\n if (action) {\n return action;\n }\n throw new WebinyError(`No action found for input type \"${type}\".`, \"NO_ACTION_FOUND\", {\n type\n });\n }\n}\n"],"mappings":";;;;;;AAQA,IAAAA,uBAAA,GAAAC,OAAA;AAGA,IAAAC,MAAA,GAAAD,OAAA;AASO,MAAME,gBAAgB,CAA8B;EAIvDC,WAAWA,CAACC,MAA+B,EAAE;IACzC,IAAI,CAACC,OAAO,GAAGD,MAAM,CAACC,OAAO;IAC7B,IAAI,CAACC,OAAO,GAAGF,MAAM,CAACE,OAAO;EACjC;EAEA,MAAaC,QAAQA,CAACC,QAAgB,EAAEC,KAAsB,EAA4B;IACtF,MAAMC,gBAAgB,GAAG,IAAAC,yDAAiC,EAACH,QAAQ,CAAC;IACpE,MAAMI,QAAQ,GAAG,MAAM,IAAI,CAACN,OAAO,CAACO,YAAY,CAACL,QAAQ,CAAC;IAE1D,MAAMM,MAAM,GAAG,IAAI,CAACC,SAAS,CAACN,KAAK,CAACO,IAAI,CAAC;IAEzC,IAAIJ,QAAQ,EAAE;MACV,OAAOE,MAAM,CAACG,UAAU,CAACL,QAAQ,EAAEH,KAAK,CAAC;IAC7C;IAEA,OAAO,MAAMK,MAAM,CAACP,QAAQ,CAAC;MACzBG,gBAAgB;MAChBF,QAAQ;MACRC;IACJ,CAAC,CAAC;EACN;EAEA,MAAaS,MAAMA,CAACC,SAAiB,EAA4B;IAC7D,MAAMC,EAAE,GAAG,IAAAT,yDAAiC,EAACQ,SAAS,CAAC;IACvD,MAAMP,QAAQ,GAAG,MAAM,IAAI,CAACN,OAAO,CAACO,YAAY,CAACO,EAAE,CAAC;IACpD,IAAI,CAACR,QAAQ,EAAE;MACX,MAAM,IAAIS,kBAAW,CACjB,qCAAqCD,EAAE,IAAI,EAC3C,4BAA4B,EAC5B;QACIA;MACJ,CACJ,CAAC;IACL;IAEA,MAAMN,MAAM,GAAG,IAAI,CAACC,SAAS,CAACH,QAAQ,CAACI,IAAI,CAAC;IAC5C,MAAMF,MAAM,CAACI,MAAM,CAACN,QAAQ,CAACQ,EAAE,CAAC;IAChC,OAAOR,QAAQ;EACnB;EAEQG,SAASA,CAACC,IAAkB,EAAmB;IACnD,MAAMF,MAAM,GAAG,IAAI,CAACT,OAAO,CAACiB,IAAI,CAACR,MAAM,IAAI;MACvC,OAAOA,MAAM,CAACS,SAAS,CAAC;QACpBP;MACJ,CAAC,CAAC;IACN,CAAC,CAAC;IACF,IAAIF,MAAM,EAAE;MACR,OAAOA,MAAM;IACjB;IACA,MAAM,IAAIO,kBAAW,CAAC,mCAAmCL,IAAI,IAAI,EAAE,iBAAiB,EAAE;MAClFA;IACJ,CAAC,CAAC;EACN;AACJ;AAACQ,OAAA,CAAAtB,gBAAA,GAAAA,gBAAA","ignoreList":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CmsModel, HeadlessCms } from "@webiny/api-headless-cms/types";
|
|
2
|
+
import type { IScheduleFetcher, IScheduleRecord, ISchedulerListParams, ISchedulerListResponse } from "./types.js";
|
|
3
|
+
export type ScheduleFetcherCms = Pick<HeadlessCms, "getEntryById" | "listLatestEntries">;
|
|
4
|
+
export interface IScheduleFetcherParams {
|
|
5
|
+
cms: ScheduleFetcherCms;
|
|
6
|
+
targetModel: CmsModel;
|
|
7
|
+
schedulerModel: CmsModel;
|
|
8
|
+
}
|
|
9
|
+
export declare class ScheduleFetcher implements IScheduleFetcher {
|
|
10
|
+
private readonly cms;
|
|
11
|
+
private readonly targetModel;
|
|
12
|
+
private readonly schedulerModel;
|
|
13
|
+
constructor(params: IScheduleFetcherParams);
|
|
14
|
+
getScheduled(targetId: string): Promise<IScheduleRecord | null>;
|
|
15
|
+
listScheduled(params: ISchedulerListParams): Promise<ISchedulerListResponse>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ScheduleFetcher = void 0;
|
|
7
|
+
var _handlerGraphql = require("@webiny/handler-graphql");
|
|
8
|
+
var _createScheduleRecordId = require("./createScheduleRecordId.js");
|
|
9
|
+
var _ScheduleRecord = require("./ScheduleRecord.js");
|
|
10
|
+
var _utils = require("@webiny/utils");
|
|
11
|
+
class ScheduleFetcher {
|
|
12
|
+
constructor(params) {
|
|
13
|
+
this.cms = params.cms;
|
|
14
|
+
this.schedulerModel = params.schedulerModel;
|
|
15
|
+
this.targetModel = params.targetModel;
|
|
16
|
+
}
|
|
17
|
+
async getScheduled(targetId) {
|
|
18
|
+
const scheduleRecordId = (0, _createScheduleRecordId.createScheduleRecordIdWithVersion)(targetId);
|
|
19
|
+
try {
|
|
20
|
+
const entry = await this.cms.getEntryById(this.schedulerModel, scheduleRecordId);
|
|
21
|
+
return (0, _ScheduleRecord.transformScheduleEntry)(this.targetModel, entry);
|
|
22
|
+
} catch (ex) {
|
|
23
|
+
if (ex.code === "NOT_FOUND" || ex instanceof _handlerGraphql.NotFoundError) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
console.error(`Error while fetching scheduled record: ${targetId}.`);
|
|
27
|
+
console.log((0, _utils.convertException)(ex));
|
|
28
|
+
throw ex;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async listScheduled(params) {
|
|
32
|
+
const [data, meta] = await this.cms.listLatestEntries(this.schedulerModel, {
|
|
33
|
+
sort: params.sort,
|
|
34
|
+
limit: params.limit,
|
|
35
|
+
/**
|
|
36
|
+
* When params
|
|
37
|
+
*/
|
|
38
|
+
where: {
|
|
39
|
+
...params.where
|
|
40
|
+
},
|
|
41
|
+
after: params.after
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
data: data.map(item => (0, _ScheduleRecord.transformScheduleEntry)(this.targetModel, item)),
|
|
45
|
+
meta
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.ScheduleFetcher = ScheduleFetcher;
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=ScheduleFetcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_handlerGraphql","require","_createScheduleRecordId","_ScheduleRecord","_utils","ScheduleFetcher","constructor","params","cms","schedulerModel","targetModel","getScheduled","targetId","scheduleRecordId","createScheduleRecordIdWithVersion","entry","getEntryById","transformScheduleEntry","ex","code","NotFoundError","console","error","log","convertException","listScheduled","data","meta","listLatestEntries","sort","limit","where","after","map","item","exports"],"sources":["ScheduleFetcher.ts"],"sourcesContent":["import type { CmsModel, HeadlessCms } from \"@webiny/api-headless-cms/types\";\nimport type {\n IScheduleEntryValues,\n IScheduleFetcher,\n IScheduleRecord,\n ISchedulerListParams,\n ISchedulerListResponse\n} from \"~/scheduler/types.js\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { createScheduleRecordIdWithVersion } from \"~/scheduler/createScheduleRecordId.js\";\nimport { transformScheduleEntry } from \"~/scheduler/ScheduleRecord.js\";\nimport { convertException } from \"@webiny/utils\";\n\nexport type ScheduleFetcherCms = Pick<HeadlessCms, \"getEntryById\" | \"listLatestEntries\">;\n\nexport interface IScheduleFetcherParams {\n cms: ScheduleFetcherCms;\n targetModel: CmsModel;\n schedulerModel: CmsModel;\n}\n\nexport class ScheduleFetcher implements IScheduleFetcher {\n private readonly cms: ScheduleFetcherCms;\n private readonly targetModel: CmsModel;\n private readonly schedulerModel: CmsModel;\n\n constructor(params: IScheduleFetcherParams) {\n this.cms = params.cms;\n this.schedulerModel = params.schedulerModel;\n this.targetModel = params.targetModel;\n }\n\n public async getScheduled(targetId: string): Promise<IScheduleRecord | null> {\n const scheduleRecordId = createScheduleRecordIdWithVersion(targetId);\n try {\n const entry = await this.cms.getEntryById<IScheduleEntryValues>(\n this.schedulerModel,\n scheduleRecordId\n );\n return transformScheduleEntry(this.targetModel, entry);\n } catch (ex) {\n if (ex.code === \"NOT_FOUND\" || ex instanceof NotFoundError) {\n return null;\n }\n console.error(`Error while fetching scheduled record: ${targetId}.`);\n console.log(convertException(ex));\n throw ex;\n }\n }\n\n public async listScheduled(params: ISchedulerListParams): Promise<ISchedulerListResponse> {\n const [data, meta] = await this.cms.listLatestEntries<IScheduleEntryValues>(\n this.schedulerModel,\n {\n sort: params.sort,\n limit: params.limit,\n /**\n * When params\n */\n where: {\n ...params.where\n },\n after: params.after\n }\n );\n\n return {\n data: data.map(item => transformScheduleEntry(this.targetModel, item)),\n meta\n };\n }\n}\n"],"mappings":";;;;;;AAQA,IAAAA,eAAA,GAAAC,OAAA;AACA,IAAAC,uBAAA,GAAAD,OAAA;AACA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AAUO,MAAMI,eAAe,CAA6B;EAKrDC,WAAWA,CAACC,MAA8B,EAAE;IACxC,IAAI,CAACC,GAAG,GAAGD,MAAM,CAACC,GAAG;IACrB,IAAI,CAACC,cAAc,GAAGF,MAAM,CAACE,cAAc;IAC3C,IAAI,CAACC,WAAW,GAAGH,MAAM,CAACG,WAAW;EACzC;EAEA,MAAaC,YAAYA,CAACC,QAAgB,EAAmC;IACzE,MAAMC,gBAAgB,GAAG,IAAAC,yDAAiC,EAACF,QAAQ,CAAC;IACpE,IAAI;MACA,MAAMG,KAAK,GAAG,MAAM,IAAI,CAACP,GAAG,CAACQ,YAAY,CACrC,IAAI,CAACP,cAAc,EACnBI,gBACJ,CAAC;MACD,OAAO,IAAAI,sCAAsB,EAAC,IAAI,CAACP,WAAW,EAAEK,KAAK,CAAC;IAC1D,CAAC,CAAC,OAAOG,EAAE,EAAE;MACT,IAAIA,EAAE,CAACC,IAAI,KAAK,WAAW,IAAID,EAAE,YAAYE,6BAAa,EAAE;QACxD,OAAO,IAAI;MACf;MACAC,OAAO,CAACC,KAAK,CAAC,0CAA0CV,QAAQ,GAAG,CAAC;MACpES,OAAO,CAACE,GAAG,CAAC,IAAAC,uBAAgB,EAACN,EAAE,CAAC,CAAC;MACjC,MAAMA,EAAE;IACZ;EACJ;EAEA,MAAaO,aAAaA,CAAClB,MAA4B,EAAmC;IACtF,MAAM,CAACmB,IAAI,EAAEC,IAAI,CAAC,GAAG,MAAM,IAAI,CAACnB,GAAG,CAACoB,iBAAiB,CACjD,IAAI,CAACnB,cAAc,EACnB;MACIoB,IAAI,EAAEtB,MAAM,CAACsB,IAAI;MACjBC,KAAK,EAAEvB,MAAM,CAACuB,KAAK;MACnB;AAChB;AACA;MACgBC,KAAK,EAAE;QACH,GAAGxB,MAAM,CAACwB;MACd,CAAC;MACDC,KAAK,EAAEzB,MAAM,CAACyB;IAClB,CACJ,CAAC;IAED,OAAO;MACHN,IAAI,EAAEA,IAAI,CAACO,GAAG,CAACC,IAAI,IAAI,IAAAjB,sCAAsB,EAAC,IAAI,CAACP,WAAW,EAAEwB,IAAI,CAAC,CAAC;MACtEP;IACJ,CAAC;EACL;AACJ;AAACQ,OAAA,CAAA9B,eAAA,GAAAA,eAAA","ignoreList":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type IScheduleEntryValues, type IScheduleRecord, type ScheduledOnType, ScheduleType } from "./types.js";
|
|
2
|
+
import type { CmsEntry, CmsIdentity, CmsModel } from "@webiny/api-headless-cms/types/index.js";
|
|
3
|
+
export interface IScheduleRecordParams {
|
|
4
|
+
id: string;
|
|
5
|
+
targetId: string;
|
|
6
|
+
model: CmsModel;
|
|
7
|
+
scheduledBy: CmsIdentity;
|
|
8
|
+
/**
|
|
9
|
+
* The date when the schedule is to be executed.
|
|
10
|
+
*/
|
|
11
|
+
scheduledOn: ScheduledOnType;
|
|
12
|
+
/**
|
|
13
|
+
* The date when the action is to be set as done.
|
|
14
|
+
* User can set publishedOn (and other relevant dates) with this parameter.
|
|
15
|
+
*/
|
|
16
|
+
type: ScheduleType;
|
|
17
|
+
title: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class ScheduleRecord implements IScheduleRecord {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly targetId: string;
|
|
22
|
+
readonly model: CmsModel;
|
|
23
|
+
readonly scheduledBy: CmsIdentity;
|
|
24
|
+
readonly publishOn: ScheduledOnType | undefined;
|
|
25
|
+
readonly unpublishOn: ScheduledOnType | undefined;
|
|
26
|
+
readonly type: ScheduleType;
|
|
27
|
+
readonly title: string;
|
|
28
|
+
constructor(record: IScheduleRecordParams);
|
|
29
|
+
}
|
|
30
|
+
export declare const createScheduleRecord: (record: IScheduleRecordParams) => IScheduleRecord;
|
|
31
|
+
export declare const transformScheduleEntry: (targetModel: CmsModel, entry: CmsEntry<IScheduleEntryValues>) => IScheduleRecord;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.transformScheduleEntry = exports.createScheduleRecord = exports.ScheduleRecord = void 0;
|
|
7
|
+
var _types = require("./types.js");
|
|
8
|
+
var _error = require("@webiny/error");
|
|
9
|
+
class ScheduleRecord {
|
|
10
|
+
// public readonly dateOn: DateOnType | undefined;
|
|
11
|
+
|
|
12
|
+
constructor(record) {
|
|
13
|
+
this.id = record.id;
|
|
14
|
+
this.targetId = record.targetId;
|
|
15
|
+
this.model = record.model;
|
|
16
|
+
this.scheduledBy = record.scheduledBy;
|
|
17
|
+
// this.dateOn = record.dateOn;
|
|
18
|
+
this.publishOn = record.type === _types.ScheduleType.publish ? record.scheduledOn : undefined;
|
|
19
|
+
this.unpublishOn = record.type === _types.ScheduleType.unpublish ? record.scheduledOn : undefined;
|
|
20
|
+
this.type = record.type;
|
|
21
|
+
this.title = record.title;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.ScheduleRecord = ScheduleRecord;
|
|
25
|
+
const createScheduleRecord = record => {
|
|
26
|
+
return new ScheduleRecord(record);
|
|
27
|
+
};
|
|
28
|
+
exports.createScheduleRecord = createScheduleRecord;
|
|
29
|
+
const transformScheduleEntry = (targetModel, entry) => {
|
|
30
|
+
let type;
|
|
31
|
+
switch (entry.values.type) {
|
|
32
|
+
case _types.ScheduleType.publish:
|
|
33
|
+
type = _types.ScheduleType.publish;
|
|
34
|
+
break;
|
|
35
|
+
case _types.ScheduleType.unpublish:
|
|
36
|
+
type = _types.ScheduleType.unpublish;
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
throw new _error.WebinyError(`Unsupported schedule type "${entry.values.type}".`, "UNSUPPORTED_SCHEDULE_TYPE", {
|
|
40
|
+
type: entry.values.type,
|
|
41
|
+
entry
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return createScheduleRecord({
|
|
45
|
+
id: entry.id,
|
|
46
|
+
type,
|
|
47
|
+
title: entry.values.title,
|
|
48
|
+
targetId: entry.values.targetId,
|
|
49
|
+
scheduledOn: new Date(entry.values.scheduledOn),
|
|
50
|
+
// dateOn: isoStringToDate(entry.values.dateOn),
|
|
51
|
+
scheduledBy: entry.savedBy,
|
|
52
|
+
model: targetModel
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
exports.transformScheduleEntry = transformScheduleEntry;
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=ScheduleRecord.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_types","require","_error","ScheduleRecord","constructor","record","id","targetId","model","scheduledBy","publishOn","type","ScheduleType","publish","scheduledOn","undefined","unpublishOn","unpublish","title","exports","createScheduleRecord","transformScheduleEntry","targetModel","entry","values","WebinyError","Date","savedBy"],"sources":["ScheduleRecord.ts"],"sourcesContent":["import {\n type IScheduleEntryValues,\n type IScheduleRecord,\n type ScheduledOnType,\n ScheduleType\n} from \"~/scheduler/types.js\";\nimport type { CmsEntry, CmsIdentity, CmsModel } from \"@webiny/api-headless-cms/types/index.js\";\nimport { WebinyError } from \"@webiny/error\";\n\nexport interface IScheduleRecordParams {\n id: string;\n targetId: string;\n model: CmsModel;\n scheduledBy: CmsIdentity;\n /**\n * The date when the schedule is to be executed.\n */\n scheduledOn: ScheduledOnType;\n /**\n * The date when the action is to be set as done.\n * User can set publishedOn (and other relevant dates) with this parameter.\n */\n // dateOn: DateOnType | undefined;\n type: ScheduleType;\n title: string;\n}\n\nexport class ScheduleRecord implements IScheduleRecord {\n public readonly id: string;\n public readonly targetId: string;\n public readonly model: CmsModel;\n public readonly scheduledBy: CmsIdentity;\n public readonly publishOn: ScheduledOnType | undefined;\n public readonly unpublishOn: ScheduledOnType | undefined;\n // public readonly dateOn: DateOnType | undefined;\n public readonly type: ScheduleType;\n public readonly title: string;\n\n public constructor(record: IScheduleRecordParams) {\n this.id = record.id;\n this.targetId = record.targetId;\n this.model = record.model;\n this.scheduledBy = record.scheduledBy;\n // this.dateOn = record.dateOn;\n this.publishOn = record.type === ScheduleType.publish ? record.scheduledOn : undefined;\n this.unpublishOn = record.type === ScheduleType.unpublish ? record.scheduledOn : undefined;\n this.type = record.type;\n this.title = record.title;\n }\n}\n\nexport const createScheduleRecord = (record: IScheduleRecordParams): IScheduleRecord => {\n return new ScheduleRecord(record);\n};\n\nexport const transformScheduleEntry = (\n targetModel: CmsModel,\n entry: CmsEntry<IScheduleEntryValues>\n): IScheduleRecord => {\n let type: ScheduleType;\n switch (entry.values.type) {\n case ScheduleType.publish:\n type = ScheduleType.publish;\n break;\n case ScheduleType.unpublish:\n type = ScheduleType.unpublish;\n break;\n default:\n throw new WebinyError(\n `Unsupported schedule type \"${entry.values.type}\".`,\n \"UNSUPPORTED_SCHEDULE_TYPE\",\n {\n type: entry.values.type,\n entry\n }\n );\n }\n return createScheduleRecord({\n id: entry.id,\n type,\n title: entry.values.title,\n targetId: entry.values.targetId,\n scheduledOn: new Date(entry.values.scheduledOn),\n // dateOn: isoStringToDate(entry.values.dateOn),\n scheduledBy: entry.savedBy,\n model: targetModel\n });\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAOA,IAAAC,MAAA,GAAAD,OAAA;AAoBO,MAAME,cAAc,CAA4B;EAOnD;;EAIOC,WAAWA,CAACC,MAA6B,EAAE;IAC9C,IAAI,CAACC,EAAE,GAAGD,MAAM,CAACC,EAAE;IACnB,IAAI,CAACC,QAAQ,GAAGF,MAAM,CAACE,QAAQ;IAC/B,IAAI,CAACC,KAAK,GAAGH,MAAM,CAACG,KAAK;IACzB,IAAI,CAACC,WAAW,GAAGJ,MAAM,CAACI,WAAW;IACrC;IACA,IAAI,CAACC,SAAS,GAAGL,MAAM,CAACM,IAAI,KAAKC,mBAAY,CAACC,OAAO,GAAGR,MAAM,CAACS,WAAW,GAAGC,SAAS;IACtF,IAAI,CAACC,WAAW,GAAGX,MAAM,CAACM,IAAI,KAAKC,mBAAY,CAACK,SAAS,GAAGZ,MAAM,CAACS,WAAW,GAAGC,SAAS;IAC1F,IAAI,CAACJ,IAAI,GAAGN,MAAM,CAACM,IAAI;IACvB,IAAI,CAACO,KAAK,GAAGb,MAAM,CAACa,KAAK;EAC7B;AACJ;AAACC,OAAA,CAAAhB,cAAA,GAAAA,cAAA;AAEM,MAAMiB,oBAAoB,GAAIf,MAA6B,IAAsB;EACpF,OAAO,IAAIF,cAAc,CAACE,MAAM,CAAC;AACrC,CAAC;AAACc,OAAA,CAAAC,oBAAA,GAAAA,oBAAA;AAEK,MAAMC,sBAAsB,GAAGA,CAClCC,WAAqB,EACrBC,KAAqC,KACnB;EAClB,IAAIZ,IAAkB;EACtB,QAAQY,KAAK,CAACC,MAAM,CAACb,IAAI;IACrB,KAAKC,mBAAY,CAACC,OAAO;MACrBF,IAAI,GAAGC,mBAAY,CAACC,OAAO;MAC3B;IACJ,KAAKD,mBAAY,CAACK,SAAS;MACvBN,IAAI,GAAGC,mBAAY,CAACK,SAAS;MAC7B;IACJ;MACI,MAAM,IAAIQ,kBAAW,CACjB,8BAA8BF,KAAK,CAACC,MAAM,CAACb,IAAI,IAAI,EACnD,2BAA2B,EAC3B;QACIA,IAAI,EAAEY,KAAK,CAACC,MAAM,CAACb,IAAI;QACvBY;MACJ,CACJ,CAAC;EACT;EACA,OAAOH,oBAAoB,CAAC;IACxBd,EAAE,EAAEiB,KAAK,CAACjB,EAAE;IACZK,IAAI;IACJO,KAAK,EAAEK,KAAK,CAACC,MAAM,CAACN,KAAK;IACzBX,QAAQ,EAAEgB,KAAK,CAACC,MAAM,CAACjB,QAAQ;IAC/BO,WAAW,EAAE,IAAIY,IAAI,CAACH,KAAK,CAACC,MAAM,CAACV,WAAW,CAAC;IAC/C;IACAL,WAAW,EAAEc,KAAK,CAACI,OAAO;IAC1BnB,KAAK,EAAEc;EACX,CAAC,CAAC;AACN,CAAC;AAACH,OAAA,CAAAE,sBAAA,GAAAA,sBAAA","ignoreList":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IScheduleExecutor, IScheduleFetcher, IScheduler, IScheduleRecord, ISchedulerInput, ISchedulerListParams, ISchedulerListResponse } from "./types.js";
|
|
2
|
+
export interface ISchedulerParams {
|
|
3
|
+
fetcher: IScheduleFetcher;
|
|
4
|
+
executor: IScheduleExecutor;
|
|
5
|
+
}
|
|
6
|
+
export declare class Scheduler implements IScheduler {
|
|
7
|
+
private readonly fetcher;
|
|
8
|
+
private readonly executor;
|
|
9
|
+
constructor(params: ISchedulerParams);
|
|
10
|
+
getScheduled(targetId: string): Promise<IScheduleRecord | null>;
|
|
11
|
+
listScheduled(params: ISchedulerListParams): Promise<ISchedulerListResponse>;
|
|
12
|
+
schedule(targetId: string, input: ISchedulerInput): Promise<IScheduleRecord>;
|
|
13
|
+
cancel(id: string): Promise<IScheduleRecord>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Scheduler = void 0;
|
|
7
|
+
class Scheduler {
|
|
8
|
+
constructor(params) {
|
|
9
|
+
this.fetcher = params.fetcher;
|
|
10
|
+
this.executor = params.executor;
|
|
11
|
+
}
|
|
12
|
+
async getScheduled(targetId) {
|
|
13
|
+
return this.fetcher.getScheduled(targetId);
|
|
14
|
+
}
|
|
15
|
+
async listScheduled(params) {
|
|
16
|
+
return this.fetcher.listScheduled(params);
|
|
17
|
+
}
|
|
18
|
+
async schedule(targetId, input) {
|
|
19
|
+
return this.executor.schedule(targetId, input);
|
|
20
|
+
}
|
|
21
|
+
async cancel(id) {
|
|
22
|
+
return this.executor.cancel(id);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.Scheduler = Scheduler;
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=Scheduler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Scheduler","constructor","params","fetcher","executor","getScheduled","targetId","listScheduled","schedule","input","cancel","id","exports"],"sources":["Scheduler.ts"],"sourcesContent":["import type {\n IScheduleExecutor,\n IScheduleFetcher,\n IScheduler,\n IScheduleRecord,\n ISchedulerInput,\n ISchedulerListParams,\n ISchedulerListResponse\n} from \"~/scheduler/types.js\";\n\nexport interface ISchedulerParams {\n fetcher: IScheduleFetcher;\n executor: IScheduleExecutor;\n}\n\nexport class Scheduler implements IScheduler {\n private readonly fetcher: IScheduleFetcher;\n private readonly executor: IScheduleExecutor;\n\n public constructor(params: ISchedulerParams) {\n this.fetcher = params.fetcher;\n this.executor = params.executor;\n }\n\n public async getScheduled(targetId: string): Promise<IScheduleRecord | null> {\n return this.fetcher.getScheduled(targetId);\n }\n\n public async listScheduled(params: ISchedulerListParams): Promise<ISchedulerListResponse> {\n return this.fetcher.listScheduled(params);\n }\n\n public async schedule(targetId: string, input: ISchedulerInput): Promise<IScheduleRecord> {\n return this.executor.schedule(targetId, input);\n }\n\n public async cancel(id: string): Promise<IScheduleRecord> {\n return this.executor.cancel(id);\n }\n}\n"],"mappings":";;;;;;AAeO,MAAMA,SAAS,CAAuB;EAIlCC,WAAWA,CAACC,MAAwB,EAAE;IACzC,IAAI,CAACC,OAAO,GAAGD,MAAM,CAACC,OAAO;IAC7B,IAAI,CAACC,QAAQ,GAAGF,MAAM,CAACE,QAAQ;EACnC;EAEA,MAAaC,YAAYA,CAACC,QAAgB,EAAmC;IACzE,OAAO,IAAI,CAACH,OAAO,CAACE,YAAY,CAACC,QAAQ,CAAC;EAC9C;EAEA,MAAaC,aAAaA,CAACL,MAA4B,EAAmC;IACtF,OAAO,IAAI,CAACC,OAAO,CAACI,aAAa,CAACL,MAAM,CAAC;EAC7C;EAEA,MAAaM,QAAQA,CAACF,QAAgB,EAAEG,KAAsB,EAA4B;IACtF,OAAO,IAAI,CAACL,QAAQ,CAACI,QAAQ,CAACF,QAAQ,EAAEG,KAAK,CAAC;EAClD;EAEA,MAAaC,MAAMA,CAACC,EAAU,EAA4B;IACtD,OAAO,IAAI,CAACP,QAAQ,CAACM,MAAM,CAACC,EAAE,CAAC;EACnC;AACJ;AAACC,OAAA,CAAAZ,SAAA,GAAAA,SAAA","ignoreList":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { IScheduleAction, IScheduleActionScheduleParams, IScheduleFetcher, IScheduleRecord, ISchedulerInput } from "../types.js";
|
|
2
|
+
import type { CmsIdentity, CmsModel, HeadlessCms } from "@webiny/api-headless-cms/types";
|
|
3
|
+
import type { ISchedulerService } from "../../service/types.js";
|
|
4
|
+
export type PublishScheduleActionCms = Pick<HeadlessCms, "getEntryById" | "publishEntry" | "createEntry" | "updateEntry" | "deleteEntry">;
|
|
5
|
+
export interface IPublishScheduleActionParams {
|
|
6
|
+
service: ISchedulerService;
|
|
7
|
+
cms: PublishScheduleActionCms;
|
|
8
|
+
targetModel: CmsModel;
|
|
9
|
+
schedulerModel: CmsModel;
|
|
10
|
+
getIdentity: () => CmsIdentity;
|
|
11
|
+
fetcher: IScheduleFetcher;
|
|
12
|
+
}
|
|
13
|
+
export declare class PublishScheduleAction implements IScheduleAction {
|
|
14
|
+
private readonly service;
|
|
15
|
+
private readonly cms;
|
|
16
|
+
private readonly targetModel;
|
|
17
|
+
private readonly schedulerModel;
|
|
18
|
+
private readonly getIdentity;
|
|
19
|
+
private readonly fetcher;
|
|
20
|
+
constructor(params: IPublishScheduleActionParams);
|
|
21
|
+
canHandle(input: ISchedulerInput): boolean;
|
|
22
|
+
schedule(params: IScheduleActionScheduleParams): Promise<IScheduleRecord>;
|
|
23
|
+
reschedule(original: IScheduleRecord, input: ISchedulerInput): Promise<IScheduleRecord>;
|
|
24
|
+
cancel(id: string): Promise<void>;
|
|
25
|
+
private getTargetEntry;
|
|
26
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.PublishScheduleAction = void 0;
|
|
7
|
+
var _types = require("../types.js");
|
|
8
|
+
var _ScheduleRecord = require("../ScheduleRecord.js");
|
|
9
|
+
var _utils = require("@webiny/utils");
|
|
10
|
+
var _dates = require("../dates.js");
|
|
11
|
+
var _handlerGraphql = require("@webiny/handler-graphql");
|
|
12
|
+
var _dateInTheFuture = require("../../utils/dateInTheFuture.js");
|
|
13
|
+
var _parseIdentifier = require("@webiny/utils/parseIdentifier.js");
|
|
14
|
+
class PublishScheduleAction {
|
|
15
|
+
constructor(params) {
|
|
16
|
+
this.service = params.service;
|
|
17
|
+
this.cms = params.cms;
|
|
18
|
+
this.targetModel = params.targetModel;
|
|
19
|
+
this.schedulerModel = params.schedulerModel;
|
|
20
|
+
this.getIdentity = params.getIdentity;
|
|
21
|
+
this.fetcher = params.fetcher;
|
|
22
|
+
}
|
|
23
|
+
canHandle(input) {
|
|
24
|
+
return input.type === _types.ScheduleType.publish;
|
|
25
|
+
}
|
|
26
|
+
async schedule(params) {
|
|
27
|
+
const {
|
|
28
|
+
targetId,
|
|
29
|
+
input,
|
|
30
|
+
scheduleRecordId
|
|
31
|
+
} = params;
|
|
32
|
+
const targetEntry = await this.getTargetEntry(targetId);
|
|
33
|
+
const title = targetEntry.values[this.targetModel.titleFieldId] || "Unknown entry title";
|
|
34
|
+
const identity = this.getIdentity();
|
|
35
|
+
const currentDate = new Date();
|
|
36
|
+
/**
|
|
37
|
+
* Immediately publish the entry if requested.
|
|
38
|
+
* No need to create a schedule entry or the service event.
|
|
39
|
+
*/
|
|
40
|
+
if (input.immediately) {
|
|
41
|
+
const publishedEntry = await this.cms.publishEntry(this.targetModel, targetId);
|
|
42
|
+
return (0, _ScheduleRecord.createScheduleRecord)({
|
|
43
|
+
id: scheduleRecordId,
|
|
44
|
+
targetId,
|
|
45
|
+
model: this.targetModel,
|
|
46
|
+
scheduledBy: publishedEntry.savedBy,
|
|
47
|
+
scheduledOn: new Date(publishedEntry.savedOn),
|
|
48
|
+
// dateOn: currentDate,
|
|
49
|
+
type: _types.ScheduleType.publish,
|
|
50
|
+
title
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* If the entry is scheduled for a date in the past, we need to update it with publish information, if user sent something.
|
|
55
|
+
* No need to create a schedule entry or the service event.
|
|
56
|
+
*/
|
|
57
|
+
//
|
|
58
|
+
else if ((0, _dateInTheFuture.dateInTheFuture)(input.scheduleOn) === false) {
|
|
59
|
+
/**
|
|
60
|
+
* We need to update the entry with publish information because we cannot update it in the publishing process.
|
|
61
|
+
*/
|
|
62
|
+
await this.cms.updateEntry(this.targetModel, targetId, {
|
|
63
|
+
firstPublishedBy: identity,
|
|
64
|
+
firstPublishedOn: (0, _dates.dateToISOString)(input.scheduleOn),
|
|
65
|
+
lastPublishedOn: (0, _dates.dateToISOString)(input.scheduleOn),
|
|
66
|
+
lastPublishedBy: identity
|
|
67
|
+
});
|
|
68
|
+
const publishedEntry = await this.cms.publishEntry(this.targetModel, targetId);
|
|
69
|
+
return (0, _ScheduleRecord.createScheduleRecord)({
|
|
70
|
+
id: scheduleRecordId,
|
|
71
|
+
targetId,
|
|
72
|
+
model: this.targetModel,
|
|
73
|
+
scheduledBy: publishedEntry.savedBy,
|
|
74
|
+
scheduledOn: currentDate,
|
|
75
|
+
type: _types.ScheduleType.publish,
|
|
76
|
+
title
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const {
|
|
80
|
+
id: scheduleEntryId
|
|
81
|
+
} = (0, _parseIdentifier.parseIdentifier)(scheduleRecordId);
|
|
82
|
+
const scheduleEntry = await this.cms.createEntry(this.schedulerModel, {
|
|
83
|
+
id: scheduleEntryId,
|
|
84
|
+
targetId,
|
|
85
|
+
targetModelId: this.targetModel.modelId,
|
|
86
|
+
title,
|
|
87
|
+
type: _types.ScheduleType.publish,
|
|
88
|
+
scheduledOn: (0, _dates.dateToISOString)(input.scheduleOn),
|
|
89
|
+
scheduledBy: identity
|
|
90
|
+
});
|
|
91
|
+
try {
|
|
92
|
+
await this.service.create({
|
|
93
|
+
id: scheduleRecordId,
|
|
94
|
+
scheduleOn: input.scheduleOn
|
|
95
|
+
});
|
|
96
|
+
} catch (ex) {
|
|
97
|
+
console.error(`Could not create service event for schedule entry: ${scheduleRecordId}. Deleting the schedule entry...`);
|
|
98
|
+
console.log((0, _utils.convertException)(ex));
|
|
99
|
+
try {
|
|
100
|
+
await this.cms.deleteEntry(this.schedulerModel, scheduleRecordId);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.error(`Error while deleting schedule entry: ${scheduleRecordId}.`);
|
|
103
|
+
console.log((0, _utils.convertException)(err));
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
throw ex;
|
|
107
|
+
}
|
|
108
|
+
return (0, _ScheduleRecord.transformScheduleEntry)(this.targetModel, scheduleEntry);
|
|
109
|
+
}
|
|
110
|
+
async reschedule(original, input) {
|
|
111
|
+
const currentDate = new Date();
|
|
112
|
+
const targetId = original.targetId;
|
|
113
|
+
const targetEntry = await this.getTargetEntry(targetId);
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* There are two cases when we can immediately publish the entry:
|
|
117
|
+
* 1. If the user requested it.
|
|
118
|
+
* 2. If the entry is scheduled for a date in the past.
|
|
119
|
+
*/
|
|
120
|
+
if (input.immediately || input.scheduleOn < currentDate) {
|
|
121
|
+
await this.cms.publishEntry(this.targetModel, targetEntry.id);
|
|
122
|
+
/**
|
|
123
|
+
* We can safely cancel the original schedule entry and the event.
|
|
124
|
+
*
|
|
125
|
+
* // TODO determine if we want to ignore the error of the cancelation.
|
|
126
|
+
*/
|
|
127
|
+
try {
|
|
128
|
+
await this.cancel(original.id);
|
|
129
|
+
} catch {
|
|
130
|
+
//
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
...original,
|
|
134
|
+
publishOn: currentDate,
|
|
135
|
+
unpublishOn: undefined
|
|
136
|
+
// dateOn: publishedEntry.lastPublishedOn
|
|
137
|
+
// ? new Date(publishedEntry.lastPublishedOn)
|
|
138
|
+
// : undefined
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
await this.cms.updateEntry(this.schedulerModel, original.id, {
|
|
142
|
+
scheduledBy: this.getIdentity(),
|
|
143
|
+
scheduledOn: (0, _dates.dateToISOString)(input.scheduleOn)
|
|
144
|
+
});
|
|
145
|
+
try {
|
|
146
|
+
await this.service.update({
|
|
147
|
+
id: original.id,
|
|
148
|
+
scheduleOn: input.scheduleOn
|
|
149
|
+
});
|
|
150
|
+
} catch (ex) {
|
|
151
|
+
throw ex;
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
...original,
|
|
155
|
+
publishOn: new Date(),
|
|
156
|
+
unpublishOn: undefined
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
async cancel(id) {
|
|
160
|
+
/**
|
|
161
|
+
* No need to do anything if the record does not exist.
|
|
162
|
+
*/
|
|
163
|
+
let scheduleEntry = null;
|
|
164
|
+
try {
|
|
165
|
+
scheduleEntry = await this.fetcher.getScheduled(id);
|
|
166
|
+
if (!scheduleEntry) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
} catch {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
await this.cms.deleteEntry(this.schedulerModel, scheduleEntry.id);
|
|
174
|
+
} catch (ex) {
|
|
175
|
+
if (ex.code === "NOT_FOUND" || ex instanceof _handlerGraphql.NotFoundError) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
console.error(`Error while deleting schedule entry: ${scheduleEntry.id}.`);
|
|
179
|
+
console.log((0, _utils.convertException)(ex));
|
|
180
|
+
throw ex;
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
183
|
+
await this.service.delete(scheduleEntry.id);
|
|
184
|
+
} catch (ex) {
|
|
185
|
+
console.error(`Error while deleting service event for schedule entry: ${scheduleEntry.id}.`);
|
|
186
|
+
console.log((0, _utils.convertException)(ex));
|
|
187
|
+
throw ex;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async getTargetEntry(id) {
|
|
191
|
+
return await this.cms.getEntryById(this.targetModel, id);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.PublishScheduleAction = PublishScheduleAction;
|
|
195
|
+
|
|
196
|
+
//# sourceMappingURL=PublishScheduleAction.js.map
|