@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.
Files changed (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -0
  3. package/constants.d.ts +11 -0
  4. package/constants.js +19 -0
  5. package/constants.js.map +1 -0
  6. package/context.d.ts +7 -0
  7. package/context.js +68 -0
  8. package/context.js.map +1 -0
  9. package/graphql/index.d.ts +3 -0
  10. package/graphql/index.js +204 -0
  11. package/graphql/index.js.map +1 -0
  12. package/graphql/schema.d.ts +217 -0
  13. package/graphql/schema.js +87 -0
  14. package/graphql/schema.js.map +1 -0
  15. package/handler/Handler.d.ts +23 -0
  16. package/handler/Handler.js +74 -0
  17. package/handler/Handler.js.map +1 -0
  18. package/handler/actions/PublishHandlerAction.d.ts +13 -0
  19. package/handler/actions/PublishHandlerAction.js +64 -0
  20. package/handler/actions/PublishHandlerAction.js.map +1 -0
  21. package/handler/actions/UnpublishHandlerAction.d.ts +13 -0
  22. package/handler/actions/UnpublishHandlerAction.js +53 -0
  23. package/handler/actions/UnpublishHandlerAction.js.map +1 -0
  24. package/handler/index.d.ts +7 -0
  25. package/handler/index.js +64 -0
  26. package/handler/index.js.map +1 -0
  27. package/handler/types.d.ts +5 -0
  28. package/handler/types.js +7 -0
  29. package/handler/types.js.map +1 -0
  30. package/hooks/index.d.ts +7 -0
  31. package/hooks/index.js +58 -0
  32. package/hooks/index.js.map +1 -0
  33. package/index.d.ts +10 -0
  34. package/index.js +29 -0
  35. package/index.js.map +1 -0
  36. package/manifest.d.ts +17 -0
  37. package/manifest.js +47 -0
  38. package/manifest.js.map +1 -0
  39. package/package.json +45 -0
  40. package/scheduler/ScheduleExecutor.d.ts +16 -0
  41. package/scheduler/ScheduleExecutor.js +55 -0
  42. package/scheduler/ScheduleExecutor.js.map +1 -0
  43. package/scheduler/ScheduleFetcher.d.ts +16 -0
  44. package/scheduler/ScheduleFetcher.js +51 -0
  45. package/scheduler/ScheduleFetcher.js.map +1 -0
  46. package/scheduler/ScheduleRecord.d.ts +31 -0
  47. package/scheduler/ScheduleRecord.js +57 -0
  48. package/scheduler/ScheduleRecord.js.map +1 -0
  49. package/scheduler/Scheduler.d.ts +14 -0
  50. package/scheduler/Scheduler.js +27 -0
  51. package/scheduler/Scheduler.js.map +1 -0
  52. package/scheduler/actions/PublishScheduleAction.d.ts +26 -0
  53. package/scheduler/actions/PublishScheduleAction.js +196 -0
  54. package/scheduler/actions/PublishScheduleAction.js.map +1 -0
  55. package/scheduler/actions/UnpublishScheduleAction.d.ts +26 -0
  56. package/scheduler/actions/UnpublishScheduleAction.js +194 -0
  57. package/scheduler/actions/UnpublishScheduleAction.js.map +1 -0
  58. package/scheduler/createScheduleRecordId.d.ts +2 -0
  59. package/scheduler/createScheduleRecordId.js +33 -0
  60. package/scheduler/createScheduleRecordId.js.map +1 -0
  61. package/scheduler/createScheduler.d.ts +12 -0
  62. package/scheduler/createScheduler.js +65 -0
  63. package/scheduler/createScheduler.js.map +1 -0
  64. package/scheduler/dates.d.ts +7 -0
  65. package/scheduler/dates.js +27 -0
  66. package/scheduler/dates.js.map +1 -0
  67. package/scheduler/model.d.ts +1 -0
  68. package/scheduler/model.js +88 -0
  69. package/scheduler/model.js.map +1 -0
  70. package/scheduler/types.d.ts +88 -0
  71. package/scheduler/types.js +19 -0
  72. package/scheduler/types.js.map +1 -0
  73. package/service/SchedulerService.d.ts +23 -0
  74. package/service/SchedulerService.js +151 -0
  75. package/service/SchedulerService.js.map +1 -0
  76. package/service/types.d.ts +19 -0
  77. package/service/types.js +7 -0
  78. package/service/types.js.map +1 -0
  79. package/types.d.ts +11 -0
  80. package/types.js +7 -0
  81. package/types.js.map +1 -0
  82. package/utils/dateInTheFuture.d.ts +6 -0
  83. package/utils/dateInTheFuture.js +19 -0
  84. package/utils/dateInTheFuture.js.map +1 -0
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.updateScheduleSchema = exports.listScheduleSchema = exports.getScheduleSchema = exports.createScheduleSchema = exports.cancelScheduleSchema = void 0;
8
+ var _zod = _interopRequireDefault(require("zod"));
9
+ var _types = require("../scheduler/types.js");
10
+ var _dates = require("../scheduler/dates.js");
11
+ const getScheduleSchema = exports.getScheduleSchema = _zod.default.object({
12
+ modelId: _zod.default.string(),
13
+ id: _zod.default.string()
14
+ });
15
+ const publishAndUnpublishSchemaType = _zod.default.nativeEnum(_types.ScheduleType);
16
+ const listScheduleSchema = exports.listScheduleSchema = _zod.default.object({
17
+ modelId: _zod.default.string(),
18
+ where: _zod.default.object({
19
+ targetId: _zod.default.string().optional(),
20
+ targetEntryId: _zod.default.string().optional(),
21
+ title_contains: _zod.default.string().optional(),
22
+ title_not_contains: _zod.default.string().optional(),
23
+ type: publishAndUnpublishSchemaType.optional(),
24
+ scheduledBy: _zod.default.string().optional(),
25
+ scheduledOn: _zod.default.date().optional().transform(value => {
26
+ if (!value) {
27
+ return undefined;
28
+ }
29
+ return (0, _dates.dateToISOString)(value);
30
+ }),
31
+ scheduledOn_gte: _zod.default.date().optional().transform(value => {
32
+ if (!value) {
33
+ return undefined;
34
+ }
35
+ return (0, _dates.dateToISOString)(value);
36
+ }),
37
+ scheduledOn_lte: _zod.default.date().optional().transform(value => {
38
+ if (!value) {
39
+ return undefined;
40
+ }
41
+ return (0, _dates.dateToISOString)(value);
42
+ })
43
+ }).optional(),
44
+ sort: _zod.default.array(_zod.default.string().refine(value => {
45
+ const [field, direction] = value.split("_");
46
+ if (!field) {
47
+ return false;
48
+ } else if (direction !== "ASC" && direction !== "DESC") {
49
+ return false;
50
+ }
51
+ return true;
52
+ })).optional(),
53
+ limit: _zod.default.number().optional(),
54
+ after: _zod.default.string().optional()
55
+ });
56
+ const dateOnSchema = _zod.default.date().optional().transform(value => {
57
+ return value instanceof Date ? value : undefined;
58
+ });
59
+ const schedulerInputSchema = _zod.default.discriminatedUnion("immediately", [_zod.default.object({
60
+ immediately: _zod.default.literal(true),
61
+ scheduleOn: _zod.default.never().optional(),
62
+ dateOn: _zod.default.date().optional(),
63
+ type: publishAndUnpublishSchemaType
64
+ }), _zod.default.object({
65
+ immediately: _zod.default.literal(false).optional(),
66
+ scheduleOn: _zod.default.date().or(_zod.default.string().transform(value => {
67
+ return new Date(value);
68
+ })),
69
+ dateOn: dateOnSchema,
70
+ type: publishAndUnpublishSchemaType
71
+ })]);
72
+ const createScheduleSchema = exports.createScheduleSchema = _zod.default.object({
73
+ modelId: _zod.default.string(),
74
+ id: _zod.default.string(),
75
+ input: schedulerInputSchema
76
+ });
77
+ const updateScheduleSchema = exports.updateScheduleSchema = _zod.default.object({
78
+ modelId: _zod.default.string(),
79
+ id: _zod.default.string(),
80
+ input: schedulerInputSchema
81
+ });
82
+ const cancelScheduleSchema = exports.cancelScheduleSchema = _zod.default.object({
83
+ modelId: _zod.default.string(),
84
+ id: _zod.default.string()
85
+ });
86
+
87
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_zod","_interopRequireDefault","require","_types","_dates","getScheduleSchema","exports","zod","object","modelId","string","id","publishAndUnpublishSchemaType","nativeEnum","ScheduleType","listScheduleSchema","where","targetId","optional","targetEntryId","title_contains","title_not_contains","type","scheduledBy","scheduledOn","date","transform","value","undefined","dateToISOString","scheduledOn_gte","scheduledOn_lte","sort","array","refine","field","direction","split","limit","number","after","dateOnSchema","Date","schedulerInputSchema","discriminatedUnion","immediately","literal","scheduleOn","never","dateOn","or","createScheduleSchema","input","updateScheduleSchema","cancelScheduleSchema"],"sources":["schema.ts"],"sourcesContent":["import zod from \"zod\";\nimport type {\n CmsEntryListSortAsc,\n CmsEntryListSortDesc\n} from \"@webiny/api-headless-cms/types/index.js\";\nimport { type DateOnType, ScheduleType } from \"~/scheduler/types.js\";\nimport { dateToISOString } from \"~/scheduler/dates.js\";\n\nexport const getScheduleSchema = zod.object({\n modelId: zod.string(),\n id: zod.string()\n});\n\nconst publishAndUnpublishSchemaType = zod.nativeEnum(ScheduleType);\n\nexport const listScheduleSchema = zod.object({\n modelId: zod.string(),\n where: zod\n .object({\n targetId: zod.string().optional(),\n targetEntryId: zod.string().optional(),\n title_contains: zod.string().optional(),\n title_not_contains: zod.string().optional(),\n type: publishAndUnpublishSchemaType.optional(),\n scheduledBy: zod.string().optional(),\n scheduledOn: zod\n .date()\n .optional()\n .transform(value => {\n if (!value) {\n return undefined;\n }\n return dateToISOString(value);\n }),\n scheduledOn_gte: zod\n .date()\n .optional()\n .transform(value => {\n if (!value) {\n return undefined;\n }\n return dateToISOString(value);\n }),\n scheduledOn_lte: zod\n .date()\n .optional()\n .transform(value => {\n if (!value) {\n return undefined;\n }\n return dateToISOString(value);\n })\n })\n .optional(),\n sort: zod\n .array(\n zod.string().refine((value): value is CmsEntryListSortAsc | CmsEntryListSortDesc => {\n const [field, direction] = value.split(\"_\");\n if (!field) {\n return false;\n } else if (direction !== \"ASC\" && direction !== \"DESC\") {\n return false;\n }\n return true;\n })\n )\n .optional(),\n limit: zod.number().optional(),\n after: zod.string().optional()\n});\n\nconst dateOnSchema = zod\n .date()\n .optional()\n .transform<DateOnType | undefined>(value => {\n return value instanceof Date ? value : undefined;\n });\n\nconst schedulerInputSchema = zod.discriminatedUnion(\"immediately\", [\n zod.object({\n immediately: zod.literal(true),\n scheduleOn: zod.never().optional(),\n dateOn: zod.date().optional(),\n type: publishAndUnpublishSchemaType\n }),\n zod.object({\n immediately: zod.literal(false).optional(),\n scheduleOn: zod.date().or(\n zod.string().transform(value => {\n return new Date(value);\n })\n ),\n dateOn: dateOnSchema,\n type: publishAndUnpublishSchemaType\n })\n]);\n\nexport const createScheduleSchema = zod.object({\n modelId: zod.string(),\n id: zod.string(),\n input: schedulerInputSchema\n});\n\nexport const updateScheduleSchema = zod.object({\n modelId: zod.string(),\n id: zod.string(),\n input: schedulerInputSchema\n});\n\nexport const cancelScheduleSchema = zod.object({\n modelId: zod.string(),\n id: zod.string()\n});\n"],"mappings":";;;;;;;AAAA,IAAAA,IAAA,GAAAC,sBAAA,CAAAC,OAAA;AAKA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AAEO,MAAMG,iBAAiB,GAAAC,OAAA,CAAAD,iBAAA,GAAGE,YAAG,CAACC,MAAM,CAAC;EACxCC,OAAO,EAAEF,YAAG,CAACG,MAAM,CAAC,CAAC;EACrBC,EAAE,EAAEJ,YAAG,CAACG,MAAM,CAAC;AACnB,CAAC,CAAC;AAEF,MAAME,6BAA6B,GAAGL,YAAG,CAACM,UAAU,CAACC,mBAAY,CAAC;AAE3D,MAAMC,kBAAkB,GAAAT,OAAA,CAAAS,kBAAA,GAAGR,YAAG,CAACC,MAAM,CAAC;EACzCC,OAAO,EAAEF,YAAG,CAACG,MAAM,CAAC,CAAC;EACrBM,KAAK,EAAET,YAAG,CACLC,MAAM,CAAC;IACJS,QAAQ,EAAEV,YAAG,CAACG,MAAM,CAAC,CAAC,CAACQ,QAAQ,CAAC,CAAC;IACjCC,aAAa,EAAEZ,YAAG,CAACG,MAAM,CAAC,CAAC,CAACQ,QAAQ,CAAC,CAAC;IACtCE,cAAc,EAAEb,YAAG,CAACG,MAAM,CAAC,CAAC,CAACQ,QAAQ,CAAC,CAAC;IACvCG,kBAAkB,EAAEd,YAAG,CAACG,MAAM,CAAC,CAAC,CAACQ,QAAQ,CAAC,CAAC;IAC3CI,IAAI,EAAEV,6BAA6B,CAACM,QAAQ,CAAC,CAAC;IAC9CK,WAAW,EAAEhB,YAAG,CAACG,MAAM,CAAC,CAAC,CAACQ,QAAQ,CAAC,CAAC;IACpCM,WAAW,EAAEjB,YAAG,CACXkB,IAAI,CAAC,CAAC,CACNP,QAAQ,CAAC,CAAC,CACVQ,SAAS,CAACC,KAAK,IAAI;MAChB,IAAI,CAACA,KAAK,EAAE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CAAC;EACrBC,EAAE,EAAEJ,YAAG,CAACG,MAAM,CAAC,CAAC;EAChB0C,KAAK,EAAET;AACX,CAAC,CAAC;AAEK,MAAMW,oBAAoB,GAAAhD,OAAA,CAAAgD,oBAAA,GAAG/C,YAAG,CAACC,MAAM,CAAC;EAC3CC,OAAO,EAAEF,YAAG,CAACG,MAAM,CAAC,CAAC;EACrBC,EAAE,EAAEJ,YAAG,CAACG,MAAM,CAAC;AACnB,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,23 @@
1
+ import { SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER } from "../constants.js";
2
+ import type { IHandlerAction } from "./types.js";
3
+ import type { ScheduleContext } from "../types.js";
4
+ export interface IHandlerParams {
5
+ actions: IHandlerAction[];
6
+ }
7
+ export interface IHandlerHandleParams {
8
+ payload: IWebinyScheduledCmsActionEvent;
9
+ cms: Pick<ScheduleContext["cms"], "scheduler" | "getEntryManager" | "getModel">;
10
+ security: Pick<ScheduleContext["security"], "setIdentity" | "withoutAuthorization">;
11
+ }
12
+ export interface IWebinyScheduledCmsActionEventValues {
13
+ id: string;
14
+ scheduleOn: string;
15
+ }
16
+ export interface IWebinyScheduledCmsActionEvent {
17
+ [SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER]: IWebinyScheduledCmsActionEventValues;
18
+ }
19
+ export declare class Handler {
20
+ private readonly actions;
21
+ constructor(params: IHandlerParams);
22
+ handle(params: IHandlerHandleParams): Promise<void>;
23
+ }
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Handler = void 0;
7
+ var _index = require("@webiny/error/index");
8
+ var _constants = require("../constants.js");
9
+ var _createIdentifier = require("@webiny/utils/createIdentifier.js");
10
+ class Handler {
11
+ constructor(params) {
12
+ this.actions = params.actions;
13
+ }
14
+ async handle(params) {
15
+ const {
16
+ payload,
17
+ cms,
18
+ security
19
+ } = params;
20
+ const values = payload[_constants.SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER];
21
+ const scheduleEntryManager = await security.withoutAuthorization(async () => {
22
+ return cms.getEntryManager(_constants.SCHEDULE_MODEL_ID);
23
+ });
24
+ const scheduleEntryId = (0, _createIdentifier.createIdentifier)({
25
+ id: values.id,
26
+ version: 1
27
+ });
28
+ /**
29
+ * Just fetch the schedule entry so we know the model it is targeting.
30
+ */
31
+ const scheduleEntry = await security.withoutAuthorization(async () => {
32
+ return scheduleEntryManager.get(scheduleEntryId);
33
+ });
34
+ /**
35
+ * We want to mock the identity of the user that scheduled this record.
36
+ */
37
+ security.setIdentity(scheduleEntry.createdBy);
38
+ const targetModel = await cms.getModel(scheduleEntry.values.targetModelId);
39
+ /**
40
+ * We want a formatted schedule record to be used later.
41
+ */
42
+ const scheduler = cms.scheduler(targetModel);
43
+ const scheduleRecord = await scheduler.getScheduled(scheduleEntryId);
44
+ /**
45
+ * Should not happen as we fetched it a few lines up, just in different format.
46
+ */
47
+ if (!scheduleRecord) {
48
+ throw new _index.WebinyError(`No schedule record found for ID: ${scheduleEntryId}`, "SCHEDULE_RECORD_NOT_FOUND", values);
49
+ }
50
+ const action = this.actions.find(action => action.canHandle(scheduleRecord));
51
+ if (!action) {
52
+ await scheduleEntryManager.update(scheduleEntryId, {
53
+ error: `No action found for schedule record ID.`
54
+ });
55
+ throw new _index.WebinyError(`No action found for schedule record ID: ${scheduleEntryId}`, "NO_ACTION_FOUND", scheduleRecord);
56
+ }
57
+ try {
58
+ await action.handle(scheduleRecord);
59
+ } catch (ex) {
60
+ console.error(`Error while handling schedule record ID: ${scheduleEntryId}`);
61
+ await scheduleEntryManager.update(scheduleEntryId, {
62
+ error: ex.message
63
+ });
64
+ throw ex;
65
+ }
66
+ /**
67
+ * Everything is ok. Delete the schedule record.
68
+ */
69
+ await scheduleEntryManager.delete(scheduleEntryId);
70
+ }
71
+ }
72
+ exports.Handler = Handler;
73
+
74
+ //# sourceMappingURL=Handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_index","require","_constants","_createIdentifier","Handler","constructor","params","actions","handle","payload","cms","security","values","SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER","scheduleEntryManager","withoutAuthorization","getEntryManager","SCHEDULE_MODEL_ID","scheduleEntryId","createIdentifier","id","version","scheduleEntry","get","setIdentity","createdBy","targetModel","getModel","targetModelId","scheduler","scheduleRecord","getScheduled","WebinyError","action","find","canHandle","update","error","ex","console","message","delete","exports"],"sources":["Handler.ts"],"sourcesContent":["import { WebinyError } from \"@webiny/error/index\";\nimport { SCHEDULE_MODEL_ID, SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER } from \"~/constants.js\";\nimport type { IHandlerAction } from \"~/handler/types.js\";\nimport type { IScheduleEntryValues } from \"~/scheduler/types.js\";\nimport type { ScheduleContext } from \"~/types.js\";\nimport { createIdentifier } from \"@webiny/utils/createIdentifier.js\";\n\nexport interface IHandlerParams {\n actions: IHandlerAction[];\n}\n\nexport interface IHandlerHandleParams {\n payload: IWebinyScheduledCmsActionEvent;\n cms: Pick<ScheduleContext[\"cms\"], \"scheduler\" | \"getEntryManager\" | \"getModel\">;\n security: Pick<ScheduleContext[\"security\"], \"setIdentity\" | \"withoutAuthorization\">;\n}\n\nexport interface IWebinyScheduledCmsActionEventValues {\n id: string; // id of the schedule record\n scheduleOn: string;\n}\n\nexport interface IWebinyScheduledCmsActionEvent {\n [SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER]: IWebinyScheduledCmsActionEventValues;\n}\n\nexport class Handler {\n private readonly actions: IHandlerAction[];\n\n public constructor(params: IHandlerParams) {\n this.actions = params.actions;\n }\n public async handle(params: IHandlerHandleParams): Promise<void> {\n const { payload, cms, security } = params;\n\n const values = payload[SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER];\n const scheduleEntryManager = await security.withoutAuthorization(async () => {\n return cms.getEntryManager<IScheduleEntryValues>(SCHEDULE_MODEL_ID);\n });\n\n const scheduleEntryId = createIdentifier({\n id: values.id,\n version: 1\n });\n /**\n * Just fetch the schedule entry so we know the model it is targeting.\n */\n const scheduleEntry = await security.withoutAuthorization(async () => {\n return scheduleEntryManager.get(scheduleEntryId);\n });\n /**\n * We want to mock the identity of the user that scheduled this record.\n */\n security.setIdentity(scheduleEntry.createdBy);\n\n const targetModel = await cms.getModel(scheduleEntry.values.targetModelId);\n /**\n * We want a formatted schedule record to be used later.\n */\n const scheduler = cms.scheduler(targetModel);\n const scheduleRecord = await scheduler.getScheduled(scheduleEntryId);\n /**\n * Should not happen as we fetched it a few lines up, just in different format.\n */\n if (!scheduleRecord) {\n throw new WebinyError(\n `No schedule record found for ID: ${scheduleEntryId}`,\n \"SCHEDULE_RECORD_NOT_FOUND\",\n values\n );\n }\n\n const action = this.actions.find(action => action.canHandle(scheduleRecord));\n if (!action) {\n await scheduleEntryManager.update(scheduleEntryId, {\n error: `No action found for schedule record ID.`\n });\n throw new WebinyError(\n `No action found for schedule record ID: ${scheduleEntryId}`,\n \"NO_ACTION_FOUND\",\n scheduleRecord\n );\n }\n\n try {\n await action.handle(scheduleRecord);\n } catch (ex) {\n console.error(`Error while handling schedule record ID: ${scheduleEntryId}`);\n await scheduleEntryManager.update(scheduleEntryId, {\n error: ex.message\n });\n throw ex;\n }\n /**\n * Everything is ok. Delete the schedule record.\n */\n await scheduleEntryManager.delete(scheduleEntryId);\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,UAAA,GAAAD,OAAA;AAIA,IAAAE,iBAAA,GAAAF,OAAA;AAqBO,MAAMG,OAAO,CAAC;EAGVC,WAAWA,CAACC,MAAsB,EAAE;IACvC,IAAI,CAACC,OAAO,GAAGD,MAAM,CAACC,OAAO;EACjC;EACA,MAAaC,MAAMA,CAACF,MAA4B,EAAiB;IAC7D,MAAM;MAAEG,OAAO;MAAEC,GAAG;MAAEC;IAAS,CAAC,GAAGL,MAAM;IAEzC,MAAMM,MAAM,GAAGH,OAAO,CAACI,gDAAq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;MACA,MAAMG,MAAM,CAACzB,MAAM,CAACsB,cAAc,CAAC;IACvC,CAAC,CAAC,OAAOQ,EAAE,EAAE;MACTC,OAAO,CAACF,KAAK,CAAC,4CAA4CnB,eAAe,EAAE,CAAC;MAC5E,MAAMJ,oBAAoB,CAACsB,MAAM,CAAClB,eAAe,EAAE;QAC/CmB,KAAK,EAAEC,EAAE,CAACE;MACd,CAAC,CAAC;MACF,MAAMF,EAAE;IACZ;IACA;AACR;AACA;IACQ,MAAMxB,oBAAoB,CAAC2B,MAAM,CAACvB,eAAe,CAAC;EACtD;AACJ;AAACwB,OAAA,CAAAtC,OAAA,GAAAA,OAAA","ignoreList":[]}
@@ -0,0 +1,13 @@
1
+ import type { IHandlerAction } from "../types.js";
2
+ import type { IScheduleRecord } from "../../scheduler/types.js";
3
+ import type { ScheduleContext } from "../../types.js";
4
+ export type IPublishHandlerActionParamsCms = Pick<ScheduleContext["cms"], "getEntryById" | "getPublishedEntriesByIds" | "publishEntry" | "unpublishEntry" | "republishEntry">;
5
+ export interface IPublishHandlerActionParams {
6
+ cms: IPublishHandlerActionParamsCms;
7
+ }
8
+ export declare class PublishHandlerAction implements IHandlerAction {
9
+ private readonly cms;
10
+ constructor(params: IPublishHandlerActionParams);
11
+ canHandle(record: Pick<IScheduleRecord, "type">): boolean;
12
+ handle(record: Pick<IScheduleRecord, "targetId" | "model">): Promise<void>;
13
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.PublishHandlerAction = void 0;
7
+ var _types = require("../../scheduler/types.js");
8
+ class PublishHandlerAction {
9
+ constructor(params) {
10
+ this.cms = params.cms;
11
+ }
12
+ canHandle(record) {
13
+ return record.type === _types.ScheduleType.publish;
14
+ }
15
+ async handle(record) {
16
+ const {
17
+ targetId,
18
+ model
19
+ } = record;
20
+ const targetEntry = await this.cms.getEntryById(model, targetId);
21
+ const [publishedTargetEntry] = await this.cms.getPublishedEntriesByIds(model, [targetEntry.id]);
22
+ /**
23
+ * There are few scenarios we must handle:
24
+ * 1. target entry is not published
25
+ * 2. target entry is already published, same revision published
26
+ * 3. target entry has a published revision, which is different that the target
27
+ */
28
+
29
+ /**
30
+ * 1. Has no published revision, so we can publish it.
31
+ */
32
+ if (!publishedTargetEntry) {
33
+ try {
34
+ await this.cms.publishEntry(model, targetEntry.id);
35
+ return;
36
+ } catch (error) {
37
+ console.error(`Failed to publish entry "${targetId}":`, error);
38
+ throw error;
39
+ }
40
+ }
41
+ /**
42
+ * 2. Target entry is already published.
43
+ */
44
+ //
45
+ else if (publishedTargetEntry.id === targetEntry.id) {
46
+ /**
47
+ * Already published, nothing to do.
48
+ * Maybe republish?
49
+ * TODO Do we throw an error here?
50
+ */
51
+ await this.cms.republishEntry(model, targetEntry.id);
52
+ return;
53
+ }
54
+ /**
55
+ * 3. Target entry has a published revision, which is different from the target.
56
+ */
57
+ //
58
+ await this.cms.unpublishEntry(model, publishedTargetEntry.id);
59
+ await this.cms.publishEntry(model, targetEntry.id);
60
+ }
61
+ }
62
+ exports.PublishHandlerAction = PublishHandlerAction;
63
+
64
+ //# sourceMappingURL=PublishHandlerAction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_types","require","PublishHandlerAction","constructor","params","cms","canHandle","record","type","ScheduleType","publish","handle","targetId","model","targetEntry","getEntryById","publishedTargetEntry","getPublishedEntriesByIds","id","publishEntry","error","console","republishEntry","unpublishEntry","exports"],"sources":["PublishHandlerAction.ts"],"sourcesContent":["import type { IHandlerAction } from \"~/handler/types.js\";\nimport type { IScheduleRecord } from \"~/scheduler/types.js\";\nimport { ScheduleType } from \"~/scheduler/types.js\";\nimport type { ScheduleContext } from \"~/types.js\";\n\nexport type IPublishHandlerActionParamsCms = Pick<\n ScheduleContext[\"cms\"],\n | \"getEntryById\"\n | \"getPublishedEntriesByIds\"\n | \"publishEntry\"\n | \"unpublishEntry\"\n | \"republishEntry\"\n>;\n\nexport interface IPublishHandlerActionParams {\n cms: IPublishHandlerActionParamsCms;\n}\n\nexport class PublishHandlerAction implements IHandlerAction {\n private readonly cms: IPublishHandlerActionParamsCms;\n\n public constructor(params: IPublishHandlerActionParams) {\n this.cms = params.cms;\n }\n\n public canHandle(record: Pick<IScheduleRecord, \"type\">): boolean {\n return record.type === ScheduleType.publish;\n }\n\n public async handle(record: Pick<IScheduleRecord, \"targetId\" | \"model\">): Promise<void> {\n const { targetId, model } = record;\n const targetEntry = await this.cms.getEntryById(model, targetId);\n const [publishedTargetEntry] = await this.cms.getPublishedEntriesByIds(model, [\n targetEntry.id\n ]);\n /**\n * There are few scenarios we must handle:\n * 1. target entry is not published\n * 2. target entry is already published, same revision published\n * 3. target entry has a published revision, which is different that the target\n */\n\n /**\n * 1. Has no published revision, so we can publish it.\n */\n if (!publishedTargetEntry) {\n try {\n await this.cms.publishEntry(model, targetEntry.id);\n return;\n } catch (error) {\n console.error(`Failed to publish entry \"${targetId}\":`, error);\n throw error;\n }\n }\n /**\n * 2. Target entry is already published.\n */\n //\n else if (publishedTargetEntry.id === targetEntry.id) {\n /**\n * Already published, nothing to do.\n * Maybe republish?\n * TODO Do we throw an error here?\n */\n await this.cms.republishEntry(model, targetEntry.id);\n return;\n }\n /**\n * 3. Target entry has a published revision, which is different from the target.\n */\n //\n await this.cms.unpublishEntry(model, publishedTargetEntry.id);\n await this.cms.publishEntry(model, targetEntry.id);\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AAgBO,MAAMC,oBAAoB,CAA2B;EAGjDC,WAAWA,CAACC,MAAmC,EAAE;IACpD,IAAI,CAACC,GAAG,GAAGD,MAAM,CAACC,GAAG;EACzB;EAEOC,SAASA,CAACC,MAAqC,EAAW;IAC7D,OAAOA,MAAM,CAACC,IAAI,KAAKC,mBAAY,CAACC,OAAO;EAC/C;EAEA,MAAaC,MAAMA,CAACJ,MAAmD,EAAiB;IACpF,MAAM;MAAEK,QAAQ;MAAEC;IAAM,CAAC,GAAGN,MAAM;IAClC,MAAMO,WAAW,GAAG,MAAM,IAAI,CAACT,GAAG,CAACU,YAAY,CAACF,KAAK,EAAED,QAAQ,CAAC;IAChE,MAAM,CAACI,oBAAoB,CAAC,GAAG,MAAM,IAAI,CAACX,GAAG,CAACY,wBAAwB,CAACJ,KAAK,EAAE,CAC1EC,WAAW,CAACI,EAAE,CACjB,CAAC;IACF;AACR;AACA;AACA;AACA;AACA;;IAEQ;AACR;AACA;IACQ,IAAI,CAACF,oBAAoB,EAAE;MACvB,IAAI;QACA,MAAM,IAAI,CAACX,GAAG,CAACc,YAAY,CAACN,KAAK,EAAEC,WAAW,CAACI,EAAE,CAAC;QAClD;MACJ,CAAC,CAAC,OAAOE,KAAK,EAAE;QACZC,OAAO,CAACD,KAAK,CAAC,4BAA4BR,QAAQ,IAAI,EAAEQ,KAAK,CAAC;QAC9D,MAAMA,KAAK;MACf;IACJ;IACA;AACR;AACA;IACQ;IAAA,KACK,IAAIJ,oBAAoB,CAACE,EAAE,KAAKJ,WAAW,CAACI,EAAE,EAAE;MACjD;AACZ;AACA;AACA;AACA;MACY,MAAM,IAAI,CAACb,GAAG,CAACiB,cAAc,CAACT,KAAK,EAAEC,WAAW,CAACI,EAAE,CAAC;MACpD;IACJ;IACA;AACR;AACA;IACQ;IACA,MAAM,IAAI,CAACb,GAAG,CAACkB,cAAc,CAACV,KAAK,EAAEG,oBAAoB,CAACE,EAAE,CAAC;IAC7D,MAAM,IAAI,CAACb,GAAG,CAACc,YAAY,CAACN,KAAK,EAAEC,WAAW,CAACI,EAAE,CAAC;EACtD;AACJ;AAACM,OAAA,CAAAtB,oBAAA,GAAAA,oBAAA","ignoreList":[]}
@@ -0,0 +1,13 @@
1
+ import type { IHandlerAction } from "../types.js";
2
+ import type { IScheduleRecord } from "../../scheduler/types.js";
3
+ import type { ScheduleContext } from "../../types.js";
4
+ export type IUnpublishHandlerActionParamsCms = Pick<ScheduleContext["cms"], "getEntryById" | "getPublishedEntriesByIds" | "unpublishEntry">;
5
+ export interface IUnpublishHandlerActionParams {
6
+ cms: IUnpublishHandlerActionParamsCms;
7
+ }
8
+ export declare class UnpublishHandlerAction implements IHandlerAction {
9
+ private readonly cms;
10
+ constructor(params: IUnpublishHandlerActionParams);
11
+ canHandle(record: Pick<IScheduleRecord, "type">): boolean;
12
+ handle(record: Pick<IScheduleRecord, "targetId" | "model">): Promise<void>;
13
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.UnpublishHandlerAction = void 0;
7
+ var _types = require("../../scheduler/types.js");
8
+ class UnpublishHandlerAction {
9
+ constructor(params) {
10
+ this.cms = params.cms;
11
+ }
12
+ canHandle(record) {
13
+ return record.type === _types.ScheduleType.unpublish;
14
+ }
15
+ async handle(record) {
16
+ const {
17
+ targetId,
18
+ model
19
+ } = record;
20
+ /**
21
+ * We need to handle the following scenarios:
22
+ * * 1. Target entry is not published, nothing to do.
23
+ * * 2. Target entry is published, so we can unpublish it.
24
+ * * 3. Target entry is published, but it's a different revision than the target.
25
+ */
26
+ const targetEntry = await this.cms.getEntryById(model, targetId);
27
+ const [publishedTargetEntry] = await this.cms.getPublishedEntriesByIds(model, [targetEntry.id]);
28
+
29
+ /**
30
+ * 1. Target entry is not published, nothing to do.
31
+ */
32
+ if (!publishedTargetEntry) {
33
+ console.warn(`Entry "${targetId}" is not published, nothing to unpublish.`);
34
+ return;
35
+ }
36
+ /**
37
+ * 2. Target entry is published, so we can unpublish it.
38
+ */
39
+ //
40
+ else if (publishedTargetEntry.id === targetId) {
41
+ await this.cms.unpublishEntry(model, targetId);
42
+ return;
43
+ }
44
+ /**
45
+ * 3. Target entry is published, but it's a different revision than the target.
46
+ * TODO determine if we really want to unpublish an entry which does not match the target ID.
47
+ */
48
+ await this.cms.unpublishEntry(model, publishedTargetEntry.id);
49
+ }
50
+ }
51
+ exports.UnpublishHandlerAction = UnpublishHandlerAction;
52
+
53
+ //# sourceMappingURL=UnpublishHandlerAction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_types","require","UnpublishHandlerAction","constructor","params","cms","canHandle","record","type","ScheduleType","unpublish","handle","targetId","model","targetEntry","getEntryById","publishedTargetEntry","getPublishedEntriesByIds","id","console","warn","unpublishEntry","exports"],"sources":["UnpublishHandlerAction.ts"],"sourcesContent":["import type { IHandlerAction } from \"~/handler/types.js\";\nimport type { IScheduleRecord } from \"~/scheduler/types.js\";\nimport { ScheduleType } from \"~/scheduler/types.js\";\nimport type { ScheduleContext } from \"~/types.js\";\n\nexport type IUnpublishHandlerActionParamsCms = Pick<\n ScheduleContext[\"cms\"],\n \"getEntryById\" | \"getPublishedEntriesByIds\" | \"unpublishEntry\"\n>;\n\nexport interface IUnpublishHandlerActionParams {\n cms: IUnpublishHandlerActionParamsCms;\n}\n\nexport class UnpublishHandlerAction implements IHandlerAction {\n private readonly cms: IUnpublishHandlerActionParamsCms;\n\n public constructor(params: IUnpublishHandlerActionParams) {\n this.cms = params.cms;\n }\n\n public canHandle(record: Pick<IScheduleRecord, \"type\">): boolean {\n return record.type === ScheduleType.unpublish;\n }\n public async handle(record: Pick<IScheduleRecord, \"targetId\" | \"model\">): Promise<void> {\n const { targetId, model } = record;\n /**\n * We need to handle the following scenarios:\n * * 1. Target entry is not published, nothing to do.\n * * 2. Target entry is published, so we can unpublish it.\n * * 3. Target entry is published, but it's a different revision than the target.\n */\n const targetEntry = await this.cms.getEntryById(model, targetId);\n const [publishedTargetEntry] = await this.cms.getPublishedEntriesByIds(model, [\n targetEntry.id\n ]);\n\n /**\n * 1. Target entry is not published, nothing to do.\n */\n if (!publishedTargetEntry) {\n console.warn(`Entry \"${targetId}\" is not published, nothing to unpublish.`);\n return;\n }\n /**\n * 2. Target entry is published, so we can unpublish it.\n */\n //\n else if (publishedTargetEntry.id === targetId) {\n await this.cms.unpublishEntry(model, targetId);\n return;\n }\n /**\n * 3. Target entry is published, but it's a different revision than the target.\n * TODO determine if we really want to unpublish an entry which does not match the target ID.\n */\n await this.cms.unpublishEntry(model, publishedTargetEntry.id);\n }\n}\n"],"mappings":";;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AAYO,MAAMC,sBAAsB,CAA2B;EAGnDC,WAAWA,CAACC,MAAqC,EAAE;IACtD,IAAI,CAACC,GAAG,GAAGD,MAAM,CAACC,GAAG;EACzB;EAEOC,SAASA,CAACC,MAAqC,EAAW;IAC7D,OAAOA,MAAM,CAACC,IAAI,KAAKC,mBAAY,CAACC,SAAS;EACjD;EACA,MAAaC,MAAMA,CAACJ,MAAmD,EAAiB;IACpF,MAAM;MAAEK,QAAQ;MAAEC;IAAM,CAAC,GAAGN,MAAM;IAClC;AACR;AACA;AACA;AACA;AACA;IACQ,MAAMO,WAAW,GAAG,MAAM,IAAI,CAACT,GAAG,CAACU,YAAY,CAACF,KAAK,EAAED,QAAQ,CAAC;IAChE,MAAM,CAACI,oBAAoB,CAAC,GAAG,MAAM,IAAI,CAACX,GAAG,CAACY,wBAAwB,CAACJ,KAAK,EAAE,CAC1EC,WAAW,CAACI,EAAE,CACjB,CAAC;;IAEF;AACR;AACA;IACQ,IAAI,CAACF,oBAAoB,EAAE;MACvBG,OAAO,CAACC,IAAI,CAAC,UAAUR,QAAQ,2CAA2C,CAAC;MAC3E;IACJ;IACA;AACR;AACA;IACQ;IAAA,KACK,IAAII,oBAAoB,CAACE,EAAE,KAAKN,QAAQ,EAAE;MAC3C,MAAM,IAAI,CAACP,GAAG,CAACgB,cAAc,CAACR,KAAK,EAAED,QAAQ,CAAC;MAC9C;IACJ;IACA;AACR;AACA;AACA;IACQ,MAAM,IAAI,CAACP,GAAG,CAACgB,cAAc,CAACR,KAAK,EAAEG,oBAAoB,CAACE,EAAE,CAAC;EACjE;AACJ;AAACI,OAAA,CAAApB,sBAAA,GAAAA,sBAAA","ignoreList":[]}
@@ -0,0 +1,7 @@
1
+ import type { HandlerFactoryParams } from "@webiny/handler-aws/types.js";
2
+ import type { IWebinyScheduledCmsActionEvent } from "./Handler.js";
3
+ import type { ScheduleContext } from "../types.js";
4
+ export interface HandlerParams extends HandlerFactoryParams {
5
+ debug?: boolean;
6
+ }
7
+ export declare const createScheduledCmsActionEventHandler: () => import("@webiny/handler-aws/raw/index.js").RawEventHandler<IWebinyScheduledCmsActionEvent, ScheduleContext, any>;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createScheduledCmsActionEventHandler = void 0;
7
+ var _registry = require("@webiny/handler-aws/registry.js");
8
+ var _sourceHandler = require("@webiny/handler-aws/sourceHandler.js");
9
+ var _index = require("@webiny/handler-aws/raw/index.js");
10
+ var _constants = require("../constants.js");
11
+ var _Handler = require("./Handler.js");
12
+ var _PublishHandlerAction = require("./actions/PublishHandlerAction.js");
13
+ var _UnpublishHandlerAction = require("./actions/UnpublishHandlerAction.js");
14
+ const canHandle = event => {
15
+ if (typeof event?.hasOwnProperty !== "function") {
16
+ return false;
17
+ } else if (!event.hasOwnProperty(_constants.SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER)) {
18
+ return false;
19
+ }
20
+ const value = event[_constants.SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER];
21
+ return !!(value?.id && value?.scheduleOn);
22
+ };
23
+ const handler = (0, _sourceHandler.createSourceHandler)({
24
+ name: "handler-aws-event-bridge-scheduled-cms-action-event",
25
+ canUse: event => {
26
+ return canHandle(event);
27
+ },
28
+ handle: async ({
29
+ params,
30
+ event,
31
+ context
32
+ }) => {
33
+ return (0, _index.createHandler)(params)(event, context);
34
+ }
35
+ });
36
+ _registry.registry.register(handler);
37
+ const createScheduledCmsActionEventHandler = () => {
38
+ return (0, _index.createEventHandler)({
39
+ canHandle: event => {
40
+ return canHandle(event);
41
+ },
42
+ handle: async params => {
43
+ const {
44
+ payload,
45
+ context
46
+ } = params;
47
+ const handler = new _Handler.Handler({
48
+ actions: [new _PublishHandlerAction.PublishHandlerAction({
49
+ cms: context.cms
50
+ }), new _UnpublishHandlerAction.UnpublishHandlerAction({
51
+ cms: context.cms
52
+ })]
53
+ });
54
+ return handler.handle({
55
+ payload,
56
+ cms: context.cms,
57
+ security: context.security
58
+ });
59
+ }
60
+ });
61
+ };
62
+ exports.createScheduledCmsActionEventHandler = createScheduledCmsActionEventHandler;
63
+
64
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_registry","require","_sourceHandler","_index","_constants","_Handler","_PublishHandlerAction","_UnpublishHandlerAction","canHandle","event","hasOwnProperty","SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER","value","id","scheduleOn","handler","createSourceHandler","name","canUse","handle","params","context","createHandler","registry","register","createScheduledCmsActionEventHandler","createEventHandler","payload","Handler","actions","PublishHandlerAction","cms","UnpublishHandlerAction","security","exports"],"sources":["index.ts"],"sourcesContent":["import { registry } from \"@webiny/handler-aws/registry.js\";\nimport type { HandlerFactoryParams } from \"@webiny/handler-aws/types.js\";\nimport { createSourceHandler } from \"@webiny/handler-aws/sourceHandler.js\";\nimport { createEventHandler, createHandler } from \"@webiny/handler-aws/raw/index.js\";\nimport { SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER } from \"~/constants.js\";\nimport type { IWebinyScheduledCmsActionEvent } from \"./Handler.js\";\nimport { Handler } from \"./Handler.js\";\nimport type { ScheduleContext } from \"~/types.js\";\nimport { PublishHandlerAction } from \"./actions/PublishHandlerAction.js\";\nimport { UnpublishHandlerAction } from \"./actions/UnpublishHandlerAction.js\";\n\nexport interface HandlerParams extends HandlerFactoryParams {\n debug?: boolean;\n}\n\nconst canHandle = (event: Partial<IWebinyScheduledCmsActionEvent>): boolean => {\n if (typeof event?.hasOwnProperty !== \"function\") {\n return false;\n } else if (!event.hasOwnProperty(SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER)) {\n return false;\n }\n\n const value = event[SCHEDULED_CMS_ACTION_EVENT_IDENTIFIER];\n return !!(value?.id && value?.scheduleOn);\n};\n\nconst handler = createSourceHandler<IWebinyScheduledCmsActionEvent, HandlerParams>({\n name: \"handler-aws-event-bridge-scheduled-cms-action-event\",\n canUse: event => {\n return canHandle(event);\n },\n handle: async ({ params, event, context }) => {\n return createHandler(params)(event, context);\n }\n});\n\nregistry.register(handler);\n\nexport const createScheduledCmsActionEventHandler = () => {\n return createEventHandler<IWebinyScheduledCmsActionEvent, ScheduleContext>({\n canHandle: event => {\n return canHandle(event);\n },\n handle: async params => {\n const { payload, context } = params;\n\n const handler = new Handler({\n actions: [\n new PublishHandlerAction({\n cms: context.cms\n }),\n new UnpublishHandlerAction({\n cms: context.cms\n })\n ]\n });\n\n return handler.handle({\n payload,\n cms: context.cms,\n security: context.security\n });\n }\n });\n};\n"],"mappings":";;;;;;AAAA,IAAAA,SAAA,GAAAC,OAAA;AAEA,IAAAC,cAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AAEA,IAAAI,QAAA,GAAAJ,OAAA;AAEA,IAAAK,qBAAA,GAAAL,OAAA;AACA,IAAAM,uBAAA,GAAAN,OAAA;AAMA,MAAMO,SAAS,GAAIC,KAA8C,IAAc;EAC3E,IAAI,OAAOA,KAAK,EAAEC,cAAc,KAAK,UAAU,EAAE;IAC7C,OAAO,KAAK;EAChB,CAAC,MAAM,IAAI,CAACD,KAAK,CAACC,cAAc,CAACC,gDAAqC,CAAC,EAAE;IACrE,OAAO,KAAK;EAChB;EAEA,MAAMC,KAAK,GAAGH,KAAK,CAACE,gDAAqC,CAAC;EAC1D,OAAO,CAAC,EAAEC,KAAK,EAAEC,EAAE,IAAID,KAAK,EAAEE,UAAU,CAAC;AAC7C,CAAC;AAED,MAAMC,OAAO,GAAG,IAAAC,kCAAmB,EAAgD;EAC/EC,IAAI,EAAE,qDAAqD;EAC3DC,MAAM,EAAET,KAAK,IAAI;IACb,OAAOD,SAAS,CAACC,KAAK,CAAC;EAC3B,CAAC;EACDU,MAAM,EAAE,MAAAA,CAAO;IAAEC,MAAM;IAAEX,KAAK;IAAEY;EAAQ,CAAC,KAAK;IAC1C,OAAO,IAAAC,oBAAa,EAACF,MAAM,CAAC,CAACX,KAAK,EAAEY,OAAO,CAAC;EAChD;AACJ,CAAC,CAAC;AAEFE,kBAAQ,CAACC,QAAQ,CAACT,OAAO,CAAC;AAEnB,MAAMU,oCAAoC,GAAGA,CAAA,KAAM;EACtD,OAAO,IAAAC,yBAAkB,EAAkD;IACvElB,SAAS,EAAEC,KAAK,IAAI;MAChB,OAAOD,SAAS,CAACC,KAAK,CAAC;IAC3B,CAAC;IACDU,MAAM,EAAE,MAAMC,MAAM,IAAI;MACpB,MAAM;QAAEO,OAAO;QAAEN;MAAQ,CAAC,GAAGD,MAAM;MAEnC,MAAML,OAAO,GAAG,IAAIa,gBAAO,CAAC;QACxBC,OAAO,EAAE,CACL,IAAIC,0CAAoB,CAAC;UACrBC,GAAG,EAAEV,OAAO,CAACU;QACjB,CAAC,CAAC,EACF,IAAIC,8CAAsB,CAAC;UACvBD,GAAG,EAAEV,OAAO,CAACU;QACjB,CAAC,CAAC;MAEV,CAAC,CAAC;MAEF,OAAOhB,OAAO,CAACI,MAAM,CAAC;QAClBQ,OAAO;QACPI,GAAG,EAAEV,OAAO,CAACU,GAAG;QAChBE,QAAQ,EAAEZ,OAAO,CAACY;MACtB,CAAC,CAAC;IACN;EACJ,CAAC,CAAC;AACN,CAAC;AAACC,OAAA,CAAAT,oCAAA,GAAAA,oCAAA","ignoreList":[]}
@@ -0,0 +1,5 @@
1
+ import type { IScheduleRecord } from "../scheduler/types.js";
2
+ export interface IHandlerAction {
3
+ canHandle(record: IScheduleRecord): boolean;
4
+ handle(record: IScheduleRecord): Promise<void>;
5
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { IScheduleRecord } from \"~/scheduler/types.js\";\n\nexport interface IHandlerAction {\n canHandle(record: IScheduleRecord): boolean;\n handle(record: IScheduleRecord): Promise<void>;\n}\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,7 @@
1
+ import type { CmsModel } from "@webiny/api-headless-cms/types/model.js";
2
+ import type { ScheduleContext } from "../types.js";
3
+ export interface IAttachLifecycleHookParams {
4
+ cms: ScheduleContext["cms"];
5
+ schedulerModel: Pick<CmsModel, "modelId">;
6
+ }
7
+ export declare const attachLifecycleHooks: (params: IAttachLifecycleHookParams) => void;
package/hooks/index.js ADDED
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.attachLifecycleHooks = void 0;
7
+ const attachLifecycleHooks = params => {
8
+ const {
9
+ cms,
10
+ schedulerModel
11
+ } = params;
12
+ const shouldContinue = model => {
13
+ if (model.modelId === schedulerModel.modelId) {
14
+ return false;
15
+ }
16
+ // TODO maybe change with a list of private models which are allowed?
17
+ else if (model.isPrivate) {
18
+ return false;
19
+ }
20
+ return true;
21
+ };
22
+ const cancel = async (model, target) => {
23
+ if (shouldContinue(model) === false) {
24
+ return;
25
+ }
26
+ const scheduler = cms.scheduler(model);
27
+ const entry = await scheduler.getScheduled(target.id);
28
+ if (!entry) {
29
+ return;
30
+ }
31
+ try {
32
+ await scheduler.cancel(entry.id);
33
+ } catch {
34
+ // does not matter
35
+ }
36
+ };
37
+ cms.onEntryAfterPublish.subscribe(async ({
38
+ entry,
39
+ model
40
+ }) => {
41
+ return cancel(model, entry);
42
+ });
43
+ cms.onEntryAfterUnpublish.subscribe(async ({
44
+ entry,
45
+ model
46
+ }) => {
47
+ return cancel(model, entry);
48
+ });
49
+ cms.onEntryAfterDelete.subscribe(async ({
50
+ entry,
51
+ model
52
+ }) => {
53
+ return cancel(model, entry);
54
+ });
55
+ };
56
+ exports.attachLifecycleHooks = attachLifecycleHooks;
57
+
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["attachLifecycleHooks","params","cms","schedulerModel","shouldContinue","model","modelId","isPrivate","cancel","target","scheduler","entry","getScheduled","id","onEntryAfterPublish","subscribe","onEntryAfterUnpublish","onEntryAfterDelete","exports"],"sources":["index.ts"],"sourcesContent":["import type { CmsModel } from \"@webiny/api-headless-cms/types/model.js\";\nimport type { CmsEntry } from \"@webiny/api-headless-cms/types/types.js\";\nimport type { ScheduleContext } from \"~/types.js\";\n\nexport interface IAttachLifecycleHookParams {\n cms: ScheduleContext[\"cms\"];\n schedulerModel: Pick<CmsModel, \"modelId\">;\n}\n\nexport const attachLifecycleHooks = (params: IAttachLifecycleHookParams): void => {\n const { cms, schedulerModel } = params;\n\n const shouldContinue = (model: Pick<CmsModel, \"modelId\" | \"isPrivate\">): boolean => {\n if (model.modelId === schedulerModel.modelId) {\n return false;\n }\n // TODO maybe change with a list of private models which are allowed?\n else if (model.isPrivate) {\n return false;\n }\n\n return true;\n };\n\n const cancel = async (model: CmsModel, target: Pick<CmsEntry, \"id\">): Promise<void> => {\n if (shouldContinue(model) === false) {\n return;\n }\n const scheduler = cms.scheduler(model);\n\n const entry = await scheduler.getScheduled(target.id);\n if (!entry) {\n return;\n }\n try {\n await scheduler.cancel(entry.id);\n } catch {\n // does not matter\n }\n };\n\n cms.onEntryAfterPublish.subscribe(async ({ entry, model }) => {\n return cancel(model, entry);\n });\n cms.onEntryAfterUnpublish.subscribe(async ({ entry, model }) => {\n return cancel(model, entry);\n });\n cms.onEntryAfterDelete.subscribe(async ({ entry, model }) => {\n return cancel(model, entry);\n });\n};\n"],"mappings":";;;;;;AASO,MAAMA,oBAAoB,GAAIC,MAAkC,IAAW;EAC9E,MAAM;IAAEC,GAAG;IAAEC;EAAe,CAAC,GAAGF,MAAM;EAEtC,MAAMG,cAAc,GAAIC,KAA8C,IAAc;IAChF,IAAIA,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import type { ICreateHeadlessCmsSchedulerContextParams } from "./context.js";
3
+ export interface ICreateHeadlessCmsScheduleParams extends ICreateHeadlessCmsSchedulerContextParams {
4
+ }
5
+ /**
6
+ * This will register both API and Handler plugins for the Headless CMS Scheduler.
7
+ * * Handler plugin will handle the scheduled CMS action event - a lambda call from the EventBridge Scheduler.
8
+ * * API plugin will provide the GraphQL API and code for managing the scheduled CMS actions.
9
+ */
10
+ export declare const createHeadlessCmsSchedule: (params: ICreateHeadlessCmsScheduleParams) => Plugin[];
package/index.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createHeadlessCmsSchedule = void 0;
7
+ var _context = require("./context.js");
8
+ var _model = require("./scheduler/model.js");
9
+ var _index = require("./graphql/index.js");
10
+ var _index2 = require("./handler/index.js");
11
+ /**
12
+ * This will register both API and Handler plugins for the Headless CMS Scheduler.
13
+ * * Handler plugin will handle the scheduled CMS action event - a lambda call from the EventBridge Scheduler.
14
+ * * API plugin will provide the GraphQL API and code for managing the scheduled CMS actions.
15
+ */
16
+ const createHeadlessCmsSchedule = params => {
17
+ return [
18
+ /**
19
+ * Handler for the Scheduled CMS Action Event.
20
+ */
21
+ (0, _index2.createScheduledCmsActionEventHandler)(),
22
+ /**
23
+ * API side of the scheduler.
24
+ */
25
+ (0, _model.createSchedulerModel)(), (0, _context.createHeadlessCmsScheduleContext)(params), (0, _index.createSchedulerGraphQL)()];
26
+ };
27
+ exports.createHeadlessCmsSchedule = createHeadlessCmsSchedule;
28
+
29
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_context","require","_model","_index","_index2","createHeadlessCmsSchedule","params","createScheduledCmsActionEventHandler","createSchedulerModel","createHeadlessCmsScheduleContext","createSchedulerGraphQL","exports"],"sources":["index.ts"],"sourcesContent":["import type { Plugin } from \"@webiny/plugins/types.js\";\nimport type { ICreateHeadlessCmsSchedulerContextParams } from \"~/context.js\";\nimport { createHeadlessCmsScheduleContext } from \"~/context.js\";\nimport { createSchedulerModel } from \"~/scheduler/model.js\";\nimport { createSchedulerGraphQL } from \"~/graphql/index.js\";\nimport { createScheduledCmsActionEventHandler } from \"~/handler/index.js\";\n\nexport interface ICreateHeadlessCmsScheduleParams extends ICreateHeadlessCmsSchedulerContextParams {\n //\n}\n\n/**\n * This will register both API and Handler plugins for the Headless CMS Scheduler.\n * * Handler plugin will handle the scheduled CMS action event - a lambda call from the EventBridge Scheduler.\n * * API plugin will provide the GraphQL API and code for managing the scheduled CMS actions.\n */\nexport const createHeadlessCmsSchedule = (params: ICreateHeadlessCmsScheduleParams): Plugin[] => {\n return [\n /**\n * Handler for the Scheduled CMS Action Event.\n */\n createScheduledCmsActionEventHandler(),\n /**\n * API side of the scheduler.\n */\n createSchedulerModel(),\n createHeadlessCmsScheduleContext(params),\n createSchedulerGraphQL()\n ];\n};\n"],"mappings":";;;;;;AAEA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,OAAA,GAAAH,OAAA;AAMA;AACA;AACA;AACA;AACA;AACO,MAAMI,yBAAyB,GAAIC,MAAwC,IAAe;EAC7F,OAAO;EACH;AACR;AACA;EACQ,IAAAC,4CAAoC,EAAC,CAAC;EACtC;AACR;AACA;EACQ,IAAAC,2BAAoB,EAAC,CAAC,EACtB,IAAAC,yCAAgC,EAACH,MAAM,CAAC,EACxC,IAAAI,6BAAsB,EAAC,CAAC,CAC3B;AACL,CAAC;AAACC,OAAA,CAAAN,yBAAA,GAAAA,yBAAA","ignoreList":[]}
package/manifest.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { DynamoDBDocument } from "@webiny/aws-sdk/client-dynamodb";
2
+ export interface IGetManifestErrorResult {
3
+ error: Error;
4
+ data?: never;
5
+ }
6
+ export interface IGetManifestSuccessResult {
7
+ data: {
8
+ lambdaArn: string;
9
+ roleArn: string;
10
+ };
11
+ error?: never;
12
+ }
13
+ export type IGetManifestResult = IGetManifestSuccessResult | IGetManifestErrorResult;
14
+ export interface IGetManifestParams {
15
+ client: DynamoDBDocument;
16
+ }
17
+ export declare const getManifest: (params: IGetManifestParams) => Promise<IGetManifestResult>;