@webiny/api-apw 0.0.0-unstable.1e66d121db

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 (232) hide show
  1. package/ContentApwSettingsPlugin.d.ts +10 -0
  2. package/ContentApwSettingsPlugin.js +17 -0
  3. package/ContentApwSettingsPlugin.js.map +1 -0
  4. package/LICENSE +21 -0
  5. package/README.md +34 -0
  6. package/crud/createChangeRequestMethods.d.ts +2 -0
  7. package/crud/createChangeRequestMethods.js +98 -0
  8. package/crud/createChangeRequestMethods.js.map +1 -0
  9. package/crud/createCommentMethods.d.ts +2 -0
  10. package/crud/createCommentMethods.js +99 -0
  11. package/crud/createCommentMethods.js.map +1 -0
  12. package/crud/createContentReviewMethods.d.ts +10 -0
  13. package/crud/createContentReviewMethods.js +552 -0
  14. package/crud/createContentReviewMethods.js.map +1 -0
  15. package/crud/createReviewerMethods.d.ts +2 -0
  16. package/crud/createReviewerMethods.js +99 -0
  17. package/crud/createReviewerMethods.js.map +1 -0
  18. package/crud/createWorkflowMethods.d.ts +2 -0
  19. package/crud/createWorkflowMethods.js +118 -0
  20. package/crud/createWorkflowMethods.js.map +1 -0
  21. package/crud/index.d.ts +2 -0
  22. package/crud/index.js +96 -0
  23. package/crud/index.js.map +1 -0
  24. package/crud/utils.d.ts +21 -0
  25. package/crud/utils.js +138 -0
  26. package/crud/utils.js.map +1 -0
  27. package/index.d.ts +4 -0
  28. package/index.js +36 -0
  29. package/index.js.map +1 -0
  30. package/package.json +76 -0
  31. package/plugins/cms/CmsEntryApwSettingsGetterPlugin.d.ts +10 -0
  32. package/plugins/cms/CmsEntryApwSettingsGetterPlugin.js +51 -0
  33. package/plugins/cms/CmsEntryApwSettingsGetterPlugin.js.map +1 -0
  34. package/plugins/cms/README.md +49 -0
  35. package/plugins/cms/apwEntryPlugins.d.ts +8 -0
  36. package/plugins/cms/apwEntryPlugins.js +46 -0
  37. package/plugins/cms/apwEntryPlugins.js.map +1 -0
  38. package/plugins/cms/index.d.ts +12 -0
  39. package/plugins/cms/index.js +37 -0
  40. package/plugins/cms/index.js.map +1 -0
  41. package/plugins/cms/linkContentReviewToEntry.d.ts +8 -0
  42. package/plugins/cms/linkContentReviewToEntry.js +108 -0
  43. package/plugins/cms/linkContentReviewToEntry.js.map +1 -0
  44. package/plugins/cms/linkWorkflowToEntry.d.ts +8 -0
  45. package/plugins/cms/linkWorkflowToEntry.js +166 -0
  46. package/plugins/cms/linkWorkflowToEntry.js.map +1 -0
  47. package/plugins/cms/triggerContentReview.d.ts +8 -0
  48. package/plugins/cms/triggerContentReview.js +59 -0
  49. package/plugins/cms/triggerContentReview.js.map +1 -0
  50. package/plugins/cms/updateContentReviewStatus.d.ts +10 -0
  51. package/plugins/cms/updateContentReviewStatus.js +97 -0
  52. package/plugins/cms/updateContentReviewStatus.js.map +1 -0
  53. package/plugins/cms/utils.d.ts +27 -0
  54. package/plugins/cms/utils.js +207 -0
  55. package/plugins/cms/utils.js.map +1 -0
  56. package/plugins/context.d.ts +5 -0
  57. package/plugins/context.js +146 -0
  58. package/plugins/context.js.map +1 -0
  59. package/plugins/graphql/changeRequest.gql.d.ts +4 -0
  60. package/plugins/graphql/changeRequest.gql.js +155 -0
  61. package/plugins/graphql/changeRequest.gql.js.map +1 -0
  62. package/plugins/graphql/comment.gql.d.ts +4 -0
  63. package/plugins/graphql/comment.gql.js +160 -0
  64. package/plugins/graphql/comment.gql.js.map +1 -0
  65. package/plugins/graphql/contentReview.gql.d.ts +4 -0
  66. package/plugins/graphql/contentReview.gql.js +382 -0
  67. package/plugins/graphql/contentReview.gql.js.map +1 -0
  68. package/plugins/graphql/reviewer.gql.d.ts +4 -0
  69. package/plugins/graphql/reviewer.gql.js +114 -0
  70. package/plugins/graphql/reviewer.gql.js.map +1 -0
  71. package/plugins/graphql/workflow.gql.d.ts +4 -0
  72. package/plugins/graphql/workflow.gql.js +205 -0
  73. package/plugins/graphql/workflow.gql.js.map +1 -0
  74. package/plugins/graphql.d.ts +4 -0
  75. package/plugins/graphql.js +95 -0
  76. package/plugins/graphql.js.map +1 -0
  77. package/plugins/hooks/createReviewerFromIdentity.d.ts +2 -0
  78. package/plugins/hooks/createReviewerFromIdentity.js +62 -0
  79. package/plugins/hooks/createReviewerFromIdentity.js.map +1 -0
  80. package/plugins/hooks/deleteChangeRequestsAfterContentReview.d.ts +2 -0
  81. package/plugins/hooks/deleteChangeRequestsAfterContentReview.js +67 -0
  82. package/plugins/hooks/deleteChangeRequestsAfterContentReview.js.map +1 -0
  83. package/plugins/hooks/deleteCommentsAfterChangeRequest.d.ts +2 -0
  84. package/plugins/hooks/deleteCommentsAfterChangeRequest.js +59 -0
  85. package/plugins/hooks/deleteCommentsAfterChangeRequest.js.map +1 -0
  86. package/plugins/hooks/index.d.ts +3 -0
  87. package/plugins/hooks/index.js +71 -0
  88. package/plugins/hooks/index.js.map +1 -0
  89. package/plugins/hooks/initializeContentReviewSteps.d.ts +2 -0
  90. package/plugins/hooks/initializeContentReviewSteps.js +86 -0
  91. package/plugins/hooks/initializeContentReviewSteps.js.map +1 -0
  92. package/plugins/hooks/listContentReviews.d.ts +10 -0
  93. package/plugins/hooks/listContentReviews.js +80 -0
  94. package/plugins/hooks/listContentReviews.js.map +1 -0
  95. package/plugins/hooks/updatePendingChangeRequests.d.ts +2 -0
  96. package/plugins/hooks/updatePendingChangeRequests.js +94 -0
  97. package/plugins/hooks/updatePendingChangeRequests.js.map +1 -0
  98. package/plugins/hooks/updateTotalComments.d.ts +3 -0
  99. package/plugins/hooks/updateTotalComments.js +153 -0
  100. package/plugins/hooks/updateTotalComments.js.map +1 -0
  101. package/plugins/hooks/validateChangeRequest.d.ts +2 -0
  102. package/plugins/hooks/validateChangeRequest.js +64 -0
  103. package/plugins/hooks/validateChangeRequest.js.map +1 -0
  104. package/plugins/hooks/validateComment.d.ts +2 -0
  105. package/plugins/hooks/validateComment.js +47 -0
  106. package/plugins/hooks/validateComment.js.map +1 -0
  107. package/plugins/hooks/validateContentReview.d.ts +2 -0
  108. package/plugins/hooks/validateContentReview.js +38 -0
  109. package/plugins/hooks/validateContentReview.js.map +1 -0
  110. package/plugins/pageBuilder/PageApwSettingsGetterPlugin.d.ts +9 -0
  111. package/plugins/pageBuilder/PageApwSettingsGetterPlugin.js +51 -0
  112. package/plugins/pageBuilder/PageApwSettingsGetterPlugin.js.map +1 -0
  113. package/plugins/pageBuilder/apwContentPagePlugins.d.ts +3 -0
  114. package/plugins/pageBuilder/apwContentPagePlugins.js +30 -0
  115. package/plugins/pageBuilder/apwContentPagePlugins.js.map +1 -0
  116. package/plugins/pageBuilder/extendPbPageSettingsSchema.d.ts +3 -0
  117. package/plugins/pageBuilder/extendPbPageSettingsSchema.js +25 -0
  118. package/plugins/pageBuilder/extendPbPageSettingsSchema.js.map +1 -0
  119. package/plugins/pageBuilder/index.d.ts +11 -0
  120. package/plugins/pageBuilder/index.js +45 -0
  121. package/plugins/pageBuilder/index.js.map +1 -0
  122. package/plugins/pageBuilder/linkContentReviewToPage.d.ts +8 -0
  123. package/plugins/pageBuilder/linkContentReviewToPage.js +105 -0
  124. package/plugins/pageBuilder/linkContentReviewToPage.js.map +1 -0
  125. package/plugins/pageBuilder/linkWorkflowToPage.d.ts +8 -0
  126. package/plugins/pageBuilder/linkWorkflowToPage.js +194 -0
  127. package/plugins/pageBuilder/linkWorkflowToPage.js.map +1 -0
  128. package/plugins/pageBuilder/triggerContentReview.d.ts +8 -0
  129. package/plugins/pageBuilder/triggerContentReview.js +50 -0
  130. package/plugins/pageBuilder/triggerContentReview.js.map +1 -0
  131. package/plugins/pageBuilder/updateContentReviewStatus.d.ts +10 -0
  132. package/plugins/pageBuilder/updateContentReviewStatus.js +79 -0
  133. package/plugins/pageBuilder/updateContentReviewStatus.js.map +1 -0
  134. package/plugins/pageBuilder/utils.d.ts +22 -0
  135. package/plugins/pageBuilder/utils.js +184 -0
  136. package/plugins/pageBuilder/utils.js.map +1 -0
  137. package/plugins/utils.d.ts +36 -0
  138. package/plugins/utils.js +193 -0
  139. package/plugins/utils.js.map +1 -0
  140. package/scheduler/createScheduleActionMethods.d.ts +2 -0
  141. package/scheduler/createScheduleActionMethods.js +149 -0
  142. package/scheduler/createScheduleActionMethods.js.map +1 -0
  143. package/scheduler/handlers/executeAction/index.d.ts +13 -0
  144. package/scheduler/handlers/executeAction/index.js +172 -0
  145. package/scheduler/handlers/executeAction/index.js.map +1 -0
  146. package/scheduler/handlers/executeAction/plugins/ApplicationGraphQL.d.ts +13 -0
  147. package/scheduler/handlers/executeAction/plugins/ApplicationGraphQL.js +17 -0
  148. package/scheduler/handlers/executeAction/plugins/ApplicationGraphQL.js.map +1 -0
  149. package/scheduler/handlers/executeAction/plugins/HeadlessCMSGraphQL.d.ts +11 -0
  150. package/scheduler/handlers/executeAction/plugins/HeadlessCMSGraphQL.js +126 -0
  151. package/scheduler/handlers/executeAction/plugins/HeadlessCMSGraphQL.js.map +1 -0
  152. package/scheduler/handlers/executeAction/plugins/PageBuilderGraphQL.d.ts +11 -0
  153. package/scheduler/handlers/executeAction/plugins/PageBuilderGraphQL.js +163 -0
  154. package/scheduler/handlers/executeAction/plugins/PageBuilderGraphQL.js.map +1 -0
  155. package/scheduler/handlers/executeAction/security.d.ts +4 -0
  156. package/scheduler/handlers/executeAction/security.js +68 -0
  157. package/scheduler/handlers/executeAction/security.js.map +1 -0
  158. package/scheduler/handlers/scheduleAction/index.d.ts +22 -0
  159. package/scheduler/handlers/scheduleAction/index.js +164 -0
  160. package/scheduler/handlers/scheduleAction/index.js.map +1 -0
  161. package/scheduler/handlers/scheduleAction/scheduleAction.utils.d.ts +24 -0
  162. package/scheduler/handlers/scheduleAction/scheduleAction.utils.js +172 -0
  163. package/scheduler/handlers/scheduleAction/scheduleAction.utils.js.map +1 -0
  164. package/scheduler/handlers/utils.d.ts +33 -0
  165. package/scheduler/handlers/utils.js +168 -0
  166. package/scheduler/handlers/utils.js.map +1 -0
  167. package/scheduler/index.d.ts +2 -0
  168. package/scheduler/index.js +27 -0
  169. package/scheduler/index.js.map +1 -0
  170. package/scheduler/types.d.ts +171 -0
  171. package/scheduler/types.js +34 -0
  172. package/scheduler/types.js.map +1 -0
  173. package/storageOperations/changeRequestStorageOperations.d.ts +3 -0
  174. package/storageOperations/changeRequestStorageOperations.js +122 -0
  175. package/storageOperations/changeRequestStorageOperations.js.map +1 -0
  176. package/storageOperations/commentStorageOperations.d.ts +3 -0
  177. package/storageOperations/commentStorageOperations.js +138 -0
  178. package/storageOperations/commentStorageOperations.js.map +1 -0
  179. package/storageOperations/contentReviewStorageOperations.d.ts +3 -0
  180. package/storageOperations/contentReviewStorageOperations.js +93 -0
  181. package/storageOperations/contentReviewStorageOperations.js.map +1 -0
  182. package/storageOperations/index.d.ts +15 -0
  183. package/storageOperations/index.js +48 -0
  184. package/storageOperations/index.js.map +1 -0
  185. package/storageOperations/models/changeRequest.model.d.ts +3 -0
  186. package/storageOperations/models/changeRequest.model.js +59 -0
  187. package/storageOperations/models/changeRequest.model.js.map +1 -0
  188. package/storageOperations/models/comment.model.d.ts +7 -0
  189. package/storageOperations/models/comment.model.js +60 -0
  190. package/storageOperations/models/comment.model.js.map +1 -0
  191. package/storageOperations/models/contentModelPluginFactory.d.ts +15 -0
  192. package/storageOperations/models/contentModelPluginFactory.js +28 -0
  193. package/storageOperations/models/contentModelPluginFactory.js.map +1 -0
  194. package/storageOperations/models/contentReview.model.d.ts +7 -0
  195. package/storageOperations/models/contentReview.model.js +258 -0
  196. package/storageOperations/models/contentReview.model.js.map +1 -0
  197. package/storageOperations/models/index.d.ts +2 -0
  198. package/storageOperations/models/index.js +115 -0
  199. package/storageOperations/models/index.js.map +1 -0
  200. package/storageOperations/models/reviewer.model.d.ts +3 -0
  201. package/storageOperations/models/reviewer.model.js +55 -0
  202. package/storageOperations/models/reviewer.model.js.map +1 -0
  203. package/storageOperations/models/utils.d.ts +3 -0
  204. package/storageOperations/models/utils.js +37 -0
  205. package/storageOperations/models/utils.js.map +1 -0
  206. package/storageOperations/models/workflow.model.d.ts +12 -0
  207. package/storageOperations/models/workflow.model.js +208 -0
  208. package/storageOperations/models/workflow.model.js.map +1 -0
  209. package/storageOperations/reviewerStorageOperations.d.ts +3 -0
  210. package/storageOperations/reviewerStorageOperations.js +89 -0
  211. package/storageOperations/reviewerStorageOperations.js.map +1 -0
  212. package/storageOperations/types.d.ts +34 -0
  213. package/storageOperations/types.js +5 -0
  214. package/storageOperations/types.js.map +1 -0
  215. package/storageOperations/workflowStorageOperations.d.ts +3 -0
  216. package/storageOperations/workflowStorageOperations.js +109 -0
  217. package/storageOperations/workflowStorageOperations.js.map +1 -0
  218. package/types.d.ts +740 -0
  219. package/types.js +69 -0
  220. package/types.js.map +1 -0
  221. package/utils/contentApwSettingsPlugin.d.ts +9 -0
  222. package/utils/contentApwSettingsPlugin.js +26 -0
  223. package/utils/contentApwSettingsPlugin.js.map +1 -0
  224. package/utils/errors.d.ts +16 -0
  225. package/utils/errors.js +75 -0
  226. package/utils/errors.js.map +1 -0
  227. package/utils/fieldResolver.d.ts +16 -0
  228. package/utils/fieldResolver.js +54 -0
  229. package/utils/fieldResolver.js.map +1 -0
  230. package/utils/resolve.d.ts +3 -0
  231. package/utils/resolve.js +18 -0
  232. package/utils/resolve.js.map +1 -0
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.PageBuilderGraphQL = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _ApplicationGraphQL = require("./ApplicationGraphQL");
13
+
14
+ var _types = require("../../../types");
15
+
16
+ const PB_PAGE_DATA_FIELD =
17
+ /* GraphQL */
18
+ `
19
+ {
20
+ id
21
+ pid
22
+ editor
23
+ category {
24
+ slug
25
+ }
26
+ version
27
+ title
28
+ path
29
+ url
30
+ content
31
+ savedOn
32
+ status
33
+ locked
34
+ publishedOn
35
+ locked
36
+ revisions {
37
+ id
38
+ status
39
+ locked
40
+ version
41
+ }
42
+ settings {
43
+ general {
44
+ snippet
45
+ tags
46
+ layout
47
+ image {
48
+ id
49
+ src
50
+ }
51
+ }
52
+ social {
53
+ meta {
54
+ property
55
+ content
56
+ }
57
+ title
58
+ description
59
+ image {
60
+ id
61
+ src
62
+ }
63
+ }
64
+ seo {
65
+ title
66
+ description
67
+ meta {
68
+ name
69
+ content
70
+ }
71
+ }
72
+ }
73
+ createdFrom
74
+ createdOn
75
+ createdBy {
76
+ id
77
+ displayName
78
+ type
79
+ }
80
+ }
81
+ `;
82
+ const ERROR_FIELD =
83
+ /* GraphQL */
84
+ `
85
+ {
86
+ code
87
+ data
88
+ message
89
+ }
90
+ `;
91
+ const PUBLISH_MUTATION =
92
+ /* GraphQL */
93
+ `
94
+ mutation PublishPage($id: ID!) {
95
+ pageBuilder {
96
+ publishPage(id: $id) {
97
+ data ${PB_PAGE_DATA_FIELD}
98
+ error ${ERROR_FIELD}
99
+ }
100
+ }
101
+ }
102
+ `;
103
+ const UNPUBLISH_MUTATION =
104
+ /* GraphQL */
105
+ `
106
+ mutation UnpublishPage($id: ID!) {
107
+ pageBuilder {
108
+ unpublishPage(id: $id) {
109
+ data ${PB_PAGE_DATA_FIELD}
110
+ error ${ERROR_FIELD}
111
+ }
112
+ }
113
+ }
114
+ `;
115
+
116
+ class PageBuilderGraphQL extends _ApplicationGraphQL.ApplicationGraphQL {
117
+ constructor(...args) {
118
+ super(...args);
119
+ (0, _defineProperty2.default)(this, "name", "apw.scheduler.applicationGraphQL.pageBuilder");
120
+ }
121
+
122
+ canUse(data) {
123
+ return data.type === _types.ApwContentTypes.PAGE;
124
+ }
125
+
126
+ getArn(settings) {
127
+ return settings.mainGraphqlFunctionArn;
128
+ }
129
+
130
+ getGraphQLBody(data) {
131
+ switch (data.action) {
132
+ case _types.ApwScheduleActionTypes.PUBLISH:
133
+ return this.getPublishBody(data);
134
+
135
+ case _types.ApwScheduleActionTypes.UNPUBLISH:
136
+ return this.getUnpublishBody(data);
137
+
138
+ default:
139
+ return null;
140
+ }
141
+ }
142
+
143
+ getPublishBody(data) {
144
+ return {
145
+ query: PUBLISH_MUTATION,
146
+ variables: {
147
+ id: data.entryId
148
+ }
149
+ };
150
+ }
151
+
152
+ getUnpublishBody(data) {
153
+ return {
154
+ query: UNPUBLISH_MUTATION,
155
+ variables: {
156
+ id: data.entryId
157
+ }
158
+ };
159
+ }
160
+
161
+ }
162
+
163
+ exports.PageBuilderGraphQL = PageBuilderGraphQL;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["PB_PAGE_DATA_FIELD","ERROR_FIELD","PUBLISH_MUTATION","UNPUBLISH_MUTATION","PageBuilderGraphQL","ApplicationGraphQL","canUse","data","type","ApwContentTypes","PAGE","getArn","settings","mainGraphqlFunctionArn","getGraphQLBody","action","ApwScheduleActionTypes","PUBLISH","getPublishBody","UNPUBLISH","getUnpublishBody","query","variables","id","entryId"],"sources":["PageBuilderGraphQL.ts"],"sourcesContent":["import { ApplicationGraphQL, ApplicationGraphQLBody } from \"./ApplicationGraphQL\";\nimport { ApwContentTypes, ApwScheduleActionData, ApwScheduleActionTypes } from \"~/scheduler/types\";\nimport { ApwSettings } from \"~/scheduler/handlers/utils\";\n\nconst PB_PAGE_DATA_FIELD = /* GraphQL */ `\n {\n id\n pid\n editor\n category {\n slug\n }\n version\n title\n path\n url\n content\n savedOn\n status\n locked\n publishedOn\n locked\n revisions {\n id\n status\n locked\n version\n }\n settings {\n general {\n snippet\n tags\n layout\n image {\n id\n src\n }\n }\n social {\n meta {\n property\n content\n }\n title\n description\n image {\n id\n src\n }\n }\n seo {\n title\n description\n meta {\n name\n content\n }\n }\n }\n createdFrom\n createdOn\n createdBy {\n id\n displayName\n type\n }\n }\n`;\n\nconst ERROR_FIELD = /* GraphQL */ `\n {\n code\n data\n message\n }\n`;\n\nconst PUBLISH_MUTATION = /* GraphQL */ `\n mutation PublishPage($id: ID!) {\n pageBuilder {\n publishPage(id: $id) {\n data ${PB_PAGE_DATA_FIELD}\n error ${ERROR_FIELD}\n }\n }\n }\n`;\n\nconst UNPUBLISH_MUTATION = /* GraphQL */ `\n mutation UnpublishPage($id: ID!) {\n pageBuilder {\n unpublishPage(id: $id) {\n data ${PB_PAGE_DATA_FIELD}\n error ${ERROR_FIELD}\n }\n }\n }\n`;\n\ninterface ApplicationGraphQLBodyVariables {\n id: string;\n}\n\nexport class PageBuilderGraphQL extends ApplicationGraphQL {\n public override name = \"apw.scheduler.applicationGraphQL.pageBuilder\";\n\n public override canUse(data: ApwScheduleActionData): boolean {\n return data.type === ApwContentTypes.PAGE;\n }\n\n public override getArn(settings: ApwSettings): string {\n return settings.mainGraphqlFunctionArn;\n }\n\n public override getGraphQLBody(data: ApwScheduleActionData): ApplicationGraphQLBody | null {\n switch (data.action) {\n case ApwScheduleActionTypes.PUBLISH:\n return this.getPublishBody(data);\n case ApwScheduleActionTypes.UNPUBLISH:\n return this.getUnpublishBody(data);\n default:\n return null;\n }\n }\n\n private getPublishBody(\n data: ApwScheduleActionData\n ): ApplicationGraphQLBody<ApplicationGraphQLBodyVariables> {\n return {\n query: PUBLISH_MUTATION,\n variables: {\n id: data.entryId\n }\n };\n }\n\n private getUnpublishBody(\n data: ApwScheduleActionData\n ): ApplicationGraphQLBody<ApplicationGraphQLBodyVariables> {\n return {\n query: UNPUBLISH_MUTATION,\n variables: {\n id: data.entryId\n }\n };\n }\n}\n"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAGA,MAAMA,kBAAkB;AAAG;AAAe;AAC1C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CA/DA;AAiEA,MAAMC,WAAW;AAAG;AAAe;AACnC;AACA;AACA;AACA;AACA;AACA,CANA;AAQA,MAAMC,gBAAgB;AAAG;AAAe;AACxC;AACA;AACA;AACA,uBAAuBF,kBAAmB;AAC1C,wBAAwBC,WAAY;AACpC;AACA;AACA;AACA,CATA;AAWA,MAAME,kBAAkB;AAAG;AAAe;AAC1C;AACA;AACA;AACA,uBAAuBH,kBAAmB;AAC1C,wBAAwBC,WAAY;AACpC;AACA;AACA;AACA,CATA;;AAeO,MAAMG,kBAAN,SAAiCC,sCAAjC,CAAoD;EAAA;IAAA;IAAA,4CAChC,8CADgC;EAAA;;EAGvCC,MAAM,CAACC,IAAD,EAAuC;IACzD,OAAOA,IAAI,CAACC,IAAL,KAAcC,sBAAA,CAAgBC,IAArC;EACH;;EAEeC,MAAM,CAACC,QAAD,EAAgC;IAClD,OAAOA,QAAQ,CAACC,sBAAhB;EACH;;EAEeC,cAAc,CAACP,IAAD,EAA6D;IACvF,QAAQA,IAAI,CAACQ,MAAb;MACI,KAAKC,6BAAA,CAAuBC,OAA5B;QACI,OAAO,KAAKC,cAAL,CAAoBX,IAApB,CAAP;;MACJ,KAAKS,6BAAA,CAAuBG,SAA5B;QACI,OAAO,KAAKC,gBAAL,CAAsBb,IAAtB,CAAP;;MACJ;QACI,OAAO,IAAP;IANR;EAQH;;EAEOW,cAAc,CAClBX,IADkB,EAEqC;IACvD,OAAO;MACHc,KAAK,EAAEnB,gBADJ;MAEHoB,SAAS,EAAE;QACPC,EAAE,EAAEhB,IAAI,CAACiB;MADF;IAFR,CAAP;EAMH;;EAEOJ,gBAAgB,CACpBb,IADoB,EAEmC;IACvD,OAAO;MACHc,KAAK,EAAElB,kBADJ;MAEHmB,SAAS,EAAE;QACPC,EAAE,EAAEhB,IAAI,CAACiB;MADF;IAFR,CAAP;EAMH;;AA1CsD"}
@@ -0,0 +1,4 @@
1
+ import { ContextPlugin } from "@webiny/api";
2
+ import { CreateApwContextParams } from "../../types";
3
+ import { ApwContext } from "../../../types";
4
+ export declare const createCustomAuth: ({ storageOperations }: CreateApwContextParams) => ContextPlugin<ApwContext>;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createCustomAuth = void 0;
7
+
8
+ var _api = require("@webiny/api");
9
+
10
+ var _utils = require("../utils");
11
+
12
+ const createCustomAuth = ({
13
+ storageOperations
14
+ }) => {
15
+ return new _api.ContextPlugin(({
16
+ security
17
+ }) => {
18
+ let hasApwToken = false;
19
+ security.addAuthenticator(async token => {
20
+ if (!token.startsWith(_utils.TOKEN_PREFIX)) {
21
+ return null;
22
+ }
23
+
24
+ const {
25
+ id,
26
+ tenant,
27
+ locale
28
+ } = (0, _utils.decodeToken)(token);
29
+ /**
30
+ * No point in going further if any piece of information is missing.
31
+ */
32
+
33
+ if (!id || !tenant || !locale) {
34
+ return null;
35
+ }
36
+ /**
37
+ * We must verify that action we are trying to execute actually exists.
38
+ */
39
+
40
+
41
+ const item = await storageOperations.get({
42
+ where: {
43
+ id,
44
+ tenant,
45
+ locale
46
+ }
47
+ });
48
+
49
+ if (!item) {
50
+ return null;
51
+ }
52
+
53
+ hasApwToken = true;
54
+ return item.createdBy;
55
+ });
56
+ security.addAuthorizer(async () => {
57
+ if (!hasApwToken) {
58
+ return null;
59
+ }
60
+
61
+ return [{
62
+ name: "*"
63
+ }];
64
+ });
65
+ });
66
+ };
67
+
68
+ exports.createCustomAuth = createCustomAuth;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createCustomAuth","storageOperations","ContextPlugin","security","hasApwToken","addAuthenticator","token","startsWith","TOKEN_PREFIX","id","tenant","locale","decodeToken","item","get","where","createdBy","addAuthorizer","name"],"sources":["security.ts"],"sourcesContent":["import { ContextPlugin } from \"@webiny/api\";\nimport { CreateApwContextParams } from \"~/scheduler/types\";\nimport { decodeToken, TOKEN_PREFIX } from \"~/scheduler/handlers/utils\";\nimport { ApwContext } from \"~/types\";\n\nexport const createCustomAuth = ({ storageOperations }: CreateApwContextParams) => {\n return new ContextPlugin<ApwContext>(({ security }) => {\n let hasApwToken = false;\n\n security.addAuthenticator(async token => {\n if (!token.startsWith(TOKEN_PREFIX)) {\n return null;\n }\n\n const { id, tenant, locale } = decodeToken(token);\n /**\n * No point in going further if any piece of information is missing.\n */\n if (!id || !tenant || !locale) {\n return null;\n }\n /**\n * We must verify that action we are trying to execute actually exists.\n */\n const item = await storageOperations.get({\n where: {\n id,\n tenant,\n locale\n }\n });\n\n if (!item) {\n return null;\n }\n\n hasApwToken = true;\n\n return item.createdBy;\n });\n\n security.addAuthorizer(async () => {\n if (!hasApwToken) {\n return null;\n }\n return [{ name: \"*\" }];\n });\n });\n};\n"],"mappings":";;;;;;;AAAA;;AAEA;;AAGO,MAAMA,gBAAgB,GAAG,CAAC;EAAEC;AAAF,CAAD,KAAmD;EAC/E,OAAO,IAAIC,kBAAJ,CAA8B,CAAC;IAAEC;EAAF,CAAD,KAAkB;IACnD,IAAIC,WAAW,GAAG,KAAlB;IAEAD,QAAQ,CAACE,gBAAT,CAA0B,MAAMC,KAAN,IAAe;MACrC,IAAI,CAACA,KAAK,CAACC,UAAN,CAAiBC,mBAAjB,CAAL,EAAqC;QACjC,OAAO,IAAP;MACH;;MAED,MAAM;QAAEC,EAAF;QAAMC,MAAN;QAAcC;MAAd,IAAyB,IAAAC,kBAAA,EAAYN,KAAZ,CAA/B;MACA;AACZ;AACA;;MACY,IAAI,CAACG,EAAD,IAAO,CAACC,MAAR,IAAkB,CAACC,MAAvB,EAA+B;QAC3B,OAAO,IAAP;MACH;MACD;AACZ;AACA;;;MACY,MAAME,IAAI,GAAG,MAAMZ,iBAAiB,CAACa,GAAlB,CAAsB;QACrCC,KAAK,EAAE;UACHN,EADG;UAEHC,MAFG;UAGHC;QAHG;MAD8B,CAAtB,CAAnB;;MAQA,IAAI,CAACE,IAAL,EAAW;QACP,OAAO,IAAP;MACH;;MAEDT,WAAW,GAAG,IAAd;MAEA,OAAOS,IAAI,CAACG,SAAZ;IACH,CA9BD;IAgCAb,QAAQ,CAACc,aAAT,CAAuB,YAAY;MAC/B,IAAI,CAACb,WAAL,EAAkB;QACd,OAAO,IAAP;MACH;;MACD,OAAO,CAAC;QAAEc,IAAI,EAAE;MAAR,CAAD,CAAP;IACH,CALD;EAMH,CAzCM,CAAP;AA0CH,CA3CM"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * This logic/file should be moved somewhere else as it is relying on AWS specific stuff.
3
+ */
4
+ import { ApwScheduleActionStorageOperations } from "../../types";
5
+ export declare enum InvocationTypes {
6
+ SCHEDULED = "scheduled"
7
+ }
8
+ export interface HandlerArgs {
9
+ datetime: string;
10
+ tenant: string;
11
+ locale: string;
12
+ invocationType?: InvocationTypes;
13
+ futureDatetime?: string;
14
+ }
15
+ export interface Configuration {
16
+ cwClient: any;
17
+ storageOperations: ApwScheduleActionStorageOperations;
18
+ handlers: {
19
+ executeAction: string;
20
+ };
21
+ }
22
+ export declare const scheduleActionHandlerPlugins: (config: Configuration) => ((import("@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin").ValueFilterPlugin[][] | import("@webiny/handler").HandlerResultPlugin<import("@webiny/handler/types").Context> | import("@webiny/api").ContextPlugin<import("@webiny/handler-db/types").DbContext>[])[] | import("@webiny/handler-aws").RawEventHandler<HandlerArgs, import("@webiny/handler/types").Context, any>)[];
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.scheduleActionHandlerPlugins = exports.InvocationTypes = void 0;
9
+
10
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
+
12
+ var _utils = require("../utils");
13
+
14
+ var _scheduleAction = require("./scheduleAction.utils");
15
+
16
+ var _handlerAws = require("@webiny/handler-aws");
17
+
18
+ /**
19
+ * This logic/file should be moved somewhere else as it is relying on AWS specific stuff.
20
+ */
21
+ let InvocationTypes;
22
+ exports.InvocationTypes = InvocationTypes;
23
+
24
+ (function (InvocationTypes) {
25
+ InvocationTypes["SCHEDULED"] = "scheduled";
26
+ })(InvocationTypes || (exports.InvocationTypes = InvocationTypes = {}));
27
+
28
+ const log = console.log;
29
+ /**
30
+ * Handler that creates a cloudwatch event rule for the schedule action workflow.
31
+ */
32
+
33
+ const createScheduleActionLambda = params => {
34
+ const {
35
+ cwClient: cloudWatchEventClient,
36
+ storageOperations,
37
+ handlers
38
+ } = params;
39
+ return (0, _handlerAws.createRawEventHandler)(async ({
40
+ payload,
41
+ context,
42
+ lambdaContext: eventContext
43
+ }) => {
44
+ try {
45
+ const {
46
+ locale,
47
+ tenant,
48
+ invocationType
49
+ } = payload;
50
+ /**
51
+ * If invocationType is "scheduled", execute the action.
52
+ */
53
+
54
+ if (invocationType === InvocationTypes.SCHEDULED) {
55
+ await (0, _scheduleAction.executeTask)({
56
+ args: payload,
57
+ lambdaName: handlers.executeAction,
58
+ handlerClient: context.handlerClient,
59
+ storageOperations
60
+ });
61
+ }
62
+ /**
63
+ * Get current scheduled task from the DB.
64
+ */
65
+
66
+
67
+ const currentTask = await storageOperations.getCurrentTask({
68
+ where: {
69
+ tenant,
70
+ locale
71
+ }
72
+ });
73
+ /**
74
+ * Get next task from the DB.
75
+ */
76
+
77
+ const [[nextItem]] = await storageOperations.list({
78
+ where: {
79
+ tenant,
80
+ locale
81
+ },
82
+ sort: ["datetime_ASC"],
83
+ limit: 1
84
+ });
85
+
86
+ if (!nextItem) {
87
+ log(`No item found.`);
88
+ return;
89
+ }
90
+
91
+ const nextTaskDatetime = nextItem.data.datetime;
92
+ const currentTaskDatetime = currentTask && currentTask.data.datetime;
93
+
94
+ if ((0, _utils.isDateTimeInNextCentury)(nextTaskDatetime)) {
95
+ log(`Already processed the task.`);
96
+ return;
97
+ }
98
+
99
+ if (!(0, _scheduleAction.shouldScheduleTask)(nextTaskDatetime, currentTaskDatetime)) {
100
+ log(`Already scheduled the task.`);
101
+ return;
102
+ }
103
+
104
+ const futureDatetime = (0, _utils.moveDateTimeToNextCentury)(nextItem.data.datetime);
105
+ /**
106
+ * Update "datetime" to a future date to mark it as scheduled.
107
+ */
108
+
109
+ log(`Update task's datetime to `, futureDatetime);
110
+ await storageOperations.update({
111
+ item: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, nextItem), {}, {
112
+ data: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, nextItem.data), {}, {
113
+ datetime: futureDatetime
114
+ })
115
+ }),
116
+ input: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, nextItem.data), {}, {
117
+ datetime: futureDatetime
118
+ })
119
+ });
120
+ /**
121
+ * Restore "datetime" of current task so that it can be schedule in next cycle.
122
+ */
123
+
124
+ if (currentTaskDatetime && (0, _utils.shouldRestoreDatetime)({
125
+ invocationType,
126
+ datetime: currentTaskDatetime
127
+ })) {
128
+ await (0, _scheduleAction.restoreDateTime)({
129
+ tenant,
130
+ locale,
131
+ task: currentTask,
132
+ storageOperations
133
+ });
134
+ }
135
+ /**
136
+ * Schedule Lambda
137
+ */
138
+
139
+
140
+ log(`Schedule Lambda Execution...`);
141
+ await (0, _scheduleAction.scheduleLambdaExecution)({
142
+ cloudWatchEventClient,
143
+ invokedFunctionArn: eventContext.invokedFunctionArn,
144
+ datetime: nextItem.data.datetime,
145
+ futureDatetime: futureDatetime,
146
+ tenant,
147
+ locale
148
+ });
149
+ /**
150
+ * Update current task.
151
+ */
152
+
153
+ await storageOperations.updateCurrentTask({
154
+ item: nextItem
155
+ });
156
+ } catch (ex) {
157
+ log("[HANDLER_CREATE_RULE] Error => ", ex); // TODO: Handler error. Maybe save it into DB.
158
+ }
159
+ });
160
+ };
161
+
162
+ const scheduleActionHandlerPlugins = config => [(0, _utils.basePlugins)(), createScheduleActionLambda(config)];
163
+
164
+ exports.scheduleActionHandlerPlugins = scheduleActionHandlerPlugins;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["InvocationTypes","log","console","createScheduleActionLambda","params","cwClient","cloudWatchEventClient","storageOperations","handlers","createRawEventHandler","payload","context","lambdaContext","eventContext","locale","tenant","invocationType","SCHEDULED","executeTask","args","lambdaName","executeAction","handlerClient","currentTask","getCurrentTask","where","nextItem","list","sort","limit","nextTaskDatetime","data","datetime","currentTaskDatetime","isDateTimeInNextCentury","shouldScheduleTask","futureDatetime","moveDateTimeToNextCentury","update","item","input","shouldRestoreDatetime","restoreDateTime","task","scheduleLambdaExecution","invokedFunctionArn","updateCurrentTask","ex","scheduleActionHandlerPlugins","config","basePlugins"],"sources":["index.ts"],"sourcesContent":["/**\n * This logic/file should be moved somewhere else as it is relying on AWS specific stuff.\n */\nimport { ApwScheduleActionStorageOperations } from \"~/scheduler/types\";\nimport {\n isDateTimeInNextCentury,\n moveDateTimeToNextCentury,\n shouldRestoreDatetime,\n basePlugins\n} from \"~/scheduler/handlers/utils\";\nimport {\n executeTask,\n shouldScheduleTask,\n restoreDateTime,\n scheduleLambdaExecution\n} from \"./scheduleAction.utils\";\nimport { createRawEventHandler } from \"@webiny/handler-aws\";\n\nexport enum InvocationTypes {\n SCHEDULED = \"scheduled\"\n}\n\nexport interface HandlerArgs {\n datetime: string;\n tenant: string;\n locale: string;\n invocationType?: InvocationTypes;\n futureDatetime?: string;\n}\n\nexport interface Configuration {\n cwClient: any;\n storageOperations: ApwScheduleActionStorageOperations;\n handlers: {\n executeAction: string;\n };\n}\n\nconst log = console.log;\n\n/**\n * Handler that creates a cloudwatch event rule for the schedule action workflow.\n */\nconst createScheduleActionLambda = (params: Configuration) => {\n const { cwClient: cloudWatchEventClient, storageOperations, handlers } = params;\n\n return createRawEventHandler<HandlerArgs>(\n async ({ payload, context, lambdaContext: eventContext }) => {\n try {\n const { locale, tenant, invocationType } = payload;\n /**\n * If invocationType is \"scheduled\", execute the action.\n */\n if (invocationType === InvocationTypes.SCHEDULED) {\n await executeTask({\n args: payload,\n lambdaName: handlers.executeAction,\n handlerClient: context.handlerClient,\n storageOperations\n });\n }\n\n /**\n * Get current scheduled task from the DB.\n */\n const currentTask = await storageOperations.getCurrentTask({\n where: {\n tenant,\n locale\n }\n });\n\n /**\n * Get next task from the DB.\n */\n const [[nextItem]] = await storageOperations.list({\n where: {\n tenant,\n locale\n },\n sort: [\"datetime_ASC\"],\n limit: 1\n });\n\n if (!nextItem) {\n log(`No item found.`);\n return;\n }\n\n const nextTaskDatetime = nextItem.data.datetime;\n const currentTaskDatetime = currentTask && currentTask.data.datetime;\n\n if (isDateTimeInNextCentury(nextTaskDatetime)) {\n log(`Already processed the task.`);\n return;\n }\n\n if (!shouldScheduleTask(nextTaskDatetime, currentTaskDatetime)) {\n log(`Already scheduled the task.`);\n return;\n }\n\n const futureDatetime = moveDateTimeToNextCentury(nextItem.data.datetime);\n /**\n * Update \"datetime\" to a future date to mark it as scheduled.\n */\n log(`Update task's datetime to `, futureDatetime);\n await storageOperations.update({\n item: {\n ...nextItem,\n data: { ...nextItem.data, datetime: futureDatetime }\n },\n input: { ...nextItem.data, datetime: futureDatetime }\n });\n /**\n * Restore \"datetime\" of current task so that it can be schedule in next cycle.\n */\n if (\n currentTaskDatetime &&\n shouldRestoreDatetime({\n invocationType,\n datetime: currentTaskDatetime\n })\n ) {\n await restoreDateTime({\n tenant,\n locale,\n task: currentTask,\n storageOperations\n });\n }\n /**\n * Schedule Lambda\n */\n log(`Schedule Lambda Execution...`);\n await scheduleLambdaExecution({\n cloudWatchEventClient,\n invokedFunctionArn: eventContext.invokedFunctionArn,\n datetime: nextItem.data.datetime,\n futureDatetime: futureDatetime,\n tenant,\n locale\n });\n /**\n * Update current task.\n */\n await storageOperations.updateCurrentTask({ item: nextItem });\n } catch (ex) {\n log(\"[HANDLER_CREATE_RULE] Error => \", ex);\n // TODO: Handler error. Maybe save it into DB.\n }\n }\n );\n};\n\nexport const scheduleActionHandlerPlugins = (config: Configuration) => [\n basePlugins(),\n createScheduleActionLambda(config)\n];\n"],"mappings":"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"}
@@ -0,0 +1,24 @@
1
+ import { ApwScheduleAction } from "../../types";
2
+ import { HandlerArgs, Configuration } from "./index";
3
+ import { ClientContext } from "@webiny/handler-client/types";
4
+ interface ScheduleLambdaExecutionParams extends Omit<HandlerArgs, "invocationType"> {
5
+ cloudWatchEventClient: any;
6
+ invokedFunctionArn: string;
7
+ }
8
+ /**
9
+ * Current API limit/quota for Cloudwatch events is sufficient for now. But, we should watch for it;
10
+ * in case of scheduling becoming the most frequently used feature.
11
+ * https://aws.amazon.com/about-aws/whats-new/2017/07/cloudwatch-events-increases-rules-and-api-requests-limits/
12
+ */
13
+ export declare function scheduleLambdaExecution({ cloudWatchEventClient, invokedFunctionArn, datetime, futureDatetime, tenant, locale }: ScheduleLambdaExecutionParams): Promise<void>;
14
+ export declare const shouldScheduleTask: (nextTaskDatetime: string, currentTaskDatetime: string | null) => boolean;
15
+ interface RestoreDateTimeParams extends Pick<Configuration, "storageOperations">, Pick<HandlerArgs, "tenant" | "locale"> {
16
+ task: ApwScheduleAction;
17
+ }
18
+ export declare const restoreDateTime: ({ locale, tenant, task: currentTask, storageOperations }: RestoreDateTimeParams) => Promise<void>;
19
+ interface ExecuteTaskParams extends Pick<ClientContext, "handlerClient">, Pick<Configuration, "storageOperations"> {
20
+ args: HandlerArgs;
21
+ lambdaName: string;
22
+ }
23
+ export declare const executeTask: ({ args, lambdaName, handlerClient, storageOperations }: ExecuteTaskParams) => Promise<void>;
24
+ export {};
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.restoreDateTime = exports.executeTask = void 0;
9
+ exports.scheduleLambdaExecution = scheduleLambdaExecution;
10
+ exports.shouldScheduleTask = void 0;
11
+
12
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
13
+
14
+ var _utils = require("../utils");
15
+
16
+ var _index = require("./index");
17
+
18
+ var _clientCloudwatchEvents = require("@aws-sdk/client-cloudwatch-events");
19
+
20
+ const log = console.log;
21
+
22
+ /**
23
+ * Current API limit/quota for Cloudwatch events is sufficient for now. But, we should watch for it;
24
+ * in case of scheduling becoming the most frequently used feature.
25
+ * https://aws.amazon.com/about-aws/whats-new/2017/07/cloudwatch-events-increases-rules-and-api-requests-limits/
26
+ */
27
+ async function scheduleLambdaExecution({
28
+ cloudWatchEventClient,
29
+ invokedFunctionArn,
30
+ datetime,
31
+ futureDatetime,
32
+ tenant,
33
+ locale
34
+ }) {
35
+ const {
36
+ eventTargetId,
37
+ eventRuleName
38
+ } = await (0, _utils.getApwSettings)();
39
+ /**
40
+ * Remove the target
41
+ */
42
+
43
+ const removeTargetsCommand = new _clientCloudwatchEvents.RemoveTargetsCommand({
44
+ Rule: eventRuleName,
45
+ Ids: [eventTargetId]
46
+ });
47
+ const removeTargetsResponse = await cloudWatchEventClient.send(removeTargetsCommand);
48
+ /**
49
+ * Log error.
50
+ */
51
+
52
+ if (typeof removeTargetsResponse.FailedEntryCount === "number" && removeTargetsResponse.FailedEntryCount !== 0) {
53
+ console.info("Failed in removing the targets!");
54
+ console.info(removeTargetsResponse.FailedEntries);
55
+ }
56
+ /**
57
+ * Delete the Rule
58
+ */
59
+
60
+
61
+ const deleteRuleCommand = new _clientCloudwatchEvents.DeleteRuleCommand({
62
+ Name: eventRuleName
63
+ });
64
+ await cloudWatchEventClient.send(deleteRuleCommand);
65
+ /**
66
+ * Create a new one.
67
+ * Min H D M DW Y
68
+ * 20 10 10 03 * 2022
69
+ */
70
+
71
+ const cronExpression = (0, _utils.dateTimeToCronExpression)(datetime);
72
+ const ruleParams = {
73
+ Name: eventRuleName,
74
+ ScheduleExpression: `cron(${cronExpression})`,
75
+ State: "ENABLED",
76
+ Description: `Enable us to schedule an action in publishing workflow at a particular datetime`
77
+ };
78
+ await cloudWatchEventClient.send(new _clientCloudwatchEvents.PutRuleCommand(ruleParams));
79
+ /**
80
+ * Add lambda as target for the rule.
81
+ */
82
+
83
+ await cloudWatchEventClient.send(new _clientCloudwatchEvents.PutTargetsCommand({
84
+ Rule: eventRuleName,
85
+ Targets: [{
86
+ Arn: invokedFunctionArn,
87
+ Id: eventTargetId,
88
+ Input: JSON.stringify({
89
+ datetime: datetime,
90
+ tenant: tenant,
91
+ locale: locale,
92
+ invocationType: _index.InvocationTypes.SCHEDULED,
93
+ futureDatetime: futureDatetime
94
+ })
95
+ }]
96
+ }));
97
+ }
98
+
99
+ const shouldScheduleTask = (nextTaskDatetime, currentTaskDatetime) => {
100
+ return !currentTaskDatetime || nextTaskDatetime < currentTaskDatetime;
101
+ };
102
+
103
+ exports.shouldScheduleTask = shouldScheduleTask;
104
+
105
+ const restoreDateTime = async ({
106
+ locale,
107
+ tenant,
108
+ task: currentTask,
109
+ storageOperations
110
+ }) => {
111
+ log(`Mark task "${currentTask.id}" as undone by restoring original "datetime".`);
112
+ const item = await storageOperations.get({
113
+ where: {
114
+ tenant,
115
+ locale,
116
+ id: currentTask.id
117
+ }
118
+ });
119
+
120
+ if (item) {
121
+ const newDateTime = (0, _utils.moveDateTimeToCurrentCentury)(item.data.datetime);
122
+ await storageOperations.update({
123
+ item: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item), {}, {
124
+ data: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item.data), {}, {
125
+ datetime: newDateTime
126
+ })
127
+ }),
128
+ input: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item.data), {}, {
129
+ datetime: newDateTime
130
+ })
131
+ });
132
+ }
133
+ };
134
+
135
+ exports.restoreDateTime = restoreDateTime;
136
+
137
+ const executeTask = async ({
138
+ args,
139
+ lambdaName,
140
+ handlerClient,
141
+ storageOperations
142
+ }) => {
143
+ log(`Executing task at: `, new Date().toISOString());
144
+
145
+ if (typeof handlerClient.invoke === "function") {
146
+ await handlerClient.invoke({
147
+ name: lambdaName,
148
+ payload: {
149
+ futureDatetime: args.futureDatetime,
150
+ datetime: args.datetime,
151
+ tenant: args.tenant,
152
+ locale: args.locale
153
+ },
154
+ await: false
155
+ });
156
+ }
157
+ /**
158
+ * Delete current schedule Task. So that, we can schedule a new one later.
159
+ */
160
+
161
+
162
+ try {
163
+ await storageOperations.deleteCurrentTask({
164
+ tenant: args.tenant,
165
+ locale: args.locale
166
+ });
167
+ } catch (e) {
168
+ console.error(e);
169
+ }
170
+ };
171
+
172
+ exports.executeTask = executeTask;