@@ -8,11 +8,18 @@ import {
8
8
} from "~/scheduler/types.js" ;
9
9
import { createScheduleRecord , transformScheduleEntry } from "~/scheduler/ScheduleRecord.js" ;
10
10
import { convertException } from "@webiny/utils" ;
11
- import type { CmsIdentity , CmsModel , HeadlessCms } from "@webiny/api-headless-cms/types" ;
11
+ import type {
12
+ CmsEntry ,
13
+ CmsEntryValues ,
14
+ CmsIdentity ,
15
+ CmsModel ,
16
+ HeadlessCms
17
+ } from "@webiny/api-headless-cms/types" ;
12
18
import type { ISchedulerService } from "~/service/types.js" ;
13
19
import { dateToISOString } from "~/scheduler/dates.js" ;
14
20
import { NotFoundError } from "@webiny/handler-graphql" ;
15
21
import { dateInTheFuture } from "~/utils/dateInTheFuture.js" ;
22
+ import { WebinyError } from "@webiny/error/index" ;
16
23
17
24
export type PublishScheduleActionCms = Pick <
18
25
HeadlessCms ,
@@ -49,7 +56,8 @@ export class PublishScheduleAction implements IScheduleAction {
49
56
public async schedule ( params : IScheduleActionScheduleParams ) : Promise < IScheduleRecord > {
50
57
const { targetId, input, scheduleRecordId } = params ;
51
58
52
- const targetEntry = await this . cms . getEntryById ( this . targetModel , targetId ) ;
59
+ const targetEntry = await this . getUpdateableTargetEntry ( targetId ) ;
60
+
53
61
const title = targetEntry . values [ this . targetModel . titleFieldId ] || "Unknown entry title" ;
54
62
const identity = this . getIdentity ( ) ;
55
63
@@ -138,16 +146,23 @@ export class PublishScheduleAction implements IScheduleAction {
138
146
) : Promise < IScheduleRecord > {
139
147
const currentDate = new Date ( ) ;
140
148
const targetId = original . targetId ;
149
+
150
+ const targetEntry = await this . getUpdateableTargetEntry ( targetId ) ;
151
+
141
152
/**
142
153
* There are two cases when we can immediately publish the entry:
143
154
* 1. If the user requested it.
144
155
* 2. If the entry is scheduled for a date in the past.
145
156
*/
146
157
if ( input . immediately || input . scheduleOn < currentDate ) {
147
- const updatedTargetEntry = await this . cms . updateEntry ( this . targetModel , targetId , {
148
- lastPublishedOn : input . dateOn ? input . dateOn . toISOString ( ) : undefined ,
149
- lastPublishedBy : this . getIdentity ( )
150
- } ) ;
158
+ const updatedTargetEntry = await this . cms . updateEntry (
159
+ this . targetModel ,
160
+ targetEntry . id ,
161
+ {
162
+ lastPublishedOn : input . dateOn ? input . dateOn . toISOString ( ) : undefined ,
163
+ lastPublishedBy : this . getIdentity ( )
164
+ }
165
+ ) ;
151
166
152
167
const publishedEntry = await this . cms . publishEntry (
153
168
this . targetModel ,
@@ -227,4 +242,18 @@ export class PublishScheduleAction implements IScheduleAction {
227
242
throw ex ;
228
243
}
229
244
}
245
+
246
+ private async getUpdateableTargetEntry < T = CmsEntryValues > ( id : string ) : Promise < CmsEntry < T > > {
247
+ const entry = await this . cms . getEntryById < T > ( this . targetModel , id ) ;
248
+ if ( entry . locked ) {
249
+ throw new WebinyError (
250
+ `Cannot schedule a publish action for entry "${ entry . id } " because it is locked.` ,
251
+ "ENTRY_LOCKED" ,
252
+ {
253
+ entryId : entry . id
254
+ }
255
+ ) ;
256
+ }
257
+ return entry ;
258
+ }
230
259
}
0 commit comments