@@ -15,7 +15,8 @@ import { validateModelEntryDataOrThrow } from "../entryDataValidation";
15
15
import { referenceFieldsMapping } from "../referenceFieldsMapping" ;
16
16
import { createIdentifier , parseIdentifier } from "@webiny/utils" ;
17
17
import WebinyError from "@webiny/error" ;
18
- import { STATUS_DRAFT } from "./statuses" ;
18
+ import { STATUS_DRAFT , STATUS_PUBLISHED , STATUS_UNPUBLISHED } from "./statuses" ;
19
+ import { AccessControl } from "~/crud/AccessControl/AccessControl" ;
19
20
20
21
type CreateEntryRevisionFromDataParams = {
21
22
sourceId : string ;
@@ -28,6 +29,7 @@ type CreateEntryRevisionFromDataParams = {
28
29
getLocale : ( ) => I18NLocale ;
29
30
originalEntry : CmsEntry ;
30
31
latestStorageEntry : CmsEntry ;
32
+ accessControl : AccessControl ;
31
33
} ;
32
34
33
35
export const createEntryRevisionFromData = async ( {
@@ -38,7 +40,8 @@ export const createEntryRevisionFromData = async ({
38
40
context,
39
41
getIdentity : getSecurityIdentity ,
40
42
originalEntry,
41
- latestStorageEntry
43
+ latestStorageEntry,
44
+ accessControl
42
45
} : CreateEntryRevisionFromDataParams ) : Promise < {
43
46
entry : CmsEntry ;
44
47
input : Record < string , any > ;
@@ -74,6 +77,74 @@ export const createEntryRevisionFromData = async ({
74
77
const currentIdentity = getSecurityIdentity ( ) ;
75
78
const currentDateTime = new Date ( ) ;
76
79
80
+ /**
81
+ * Users can set the initial status of the entry. If so, we need to make
82
+ * sure they have the required permissions and also that all the fields
83
+ * are filled in correctly.
84
+ */
85
+ const status = rawInput . status || STATUS_DRAFT ;
86
+ if ( status !== STATUS_DRAFT ) {
87
+ if ( status === STATUS_PUBLISHED ) {
88
+ await accessControl . ensureCanAccessEntry ( { model, pw : "p" } ) ;
89
+ } else if ( status === STATUS_UNPUBLISHED ) {
90
+ // If setting the status other than draft, we have to check if the user has permissions to publish.
91
+ await accessControl . ensureCanAccessEntry ( { model, pw : "u" } ) ;
92
+ }
93
+ }
94
+
95
+ const locked = status !== STATUS_DRAFT ;
96
+
97
+ let revisionLevelPublishingMetaFields : Pick <
98
+ CmsEntry ,
99
+ | "revisionFirstPublishedOn"
100
+ | "revisionLastPublishedOn"
101
+ | "revisionFirstPublishedBy"
102
+ | "revisionLastPublishedBy"
103
+ > = {
104
+ revisionFirstPublishedOn : getDate ( rawInput . revisionFirstPublishedOn , null ) ,
105
+ revisionLastPublishedOn : getDate ( rawInput . revisionLastPublishedOn , null ) ,
106
+ revisionFirstPublishedBy : getIdentity ( rawInput . revisionFirstPublishedBy , null ) ,
107
+ revisionLastPublishedBy : getIdentity ( rawInput . revisionLastPublishedBy , null )
108
+ } ;
109
+
110
+ let entryLevelPublishingMetaFields : Pick <
111
+ CmsEntry ,
112
+ "firstPublishedOn" | "lastPublishedOn" | "firstPublishedBy" | "lastPublishedBy"
113
+ > = {
114
+ firstPublishedOn : getDate ( rawInput . firstPublishedOn , latestStorageEntry . firstPublishedOn ) ,
115
+ lastPublishedOn : getDate ( rawInput . lastPublishedOn , latestStorageEntry . lastPublishedOn ) ,
116
+ firstPublishedBy : getIdentity (
117
+ rawInput . firstPublishedBy ,
118
+ latestStorageEntry . firstPublishedBy
119
+ ) ,
120
+ lastPublishedBy : getIdentity ( rawInput . lastPublishedBy , latestStorageEntry . lastPublishedBy )
121
+ } ;
122
+
123
+ if ( status === STATUS_PUBLISHED ) {
124
+ revisionLevelPublishingMetaFields = {
125
+ revisionFirstPublishedOn : getDate ( rawInput . revisionFirstPublishedOn , currentDateTime ) ,
126
+ revisionLastPublishedOn : getDate ( rawInput . revisionLastPublishedOn , currentDateTime ) ,
127
+ revisionFirstPublishedBy : getIdentity (
128
+ rawInput . revisionFirstPublishedBy ,
129
+ currentIdentity
130
+ ) ,
131
+ revisionLastPublishedBy : getIdentity ( rawInput . revisionLastPublishedBy , currentIdentity )
132
+ } ;
133
+
134
+ entryLevelPublishingMetaFields = {
135
+ firstPublishedOn : getDate (
136
+ rawInput . firstPublishedOn ,
137
+ latestStorageEntry . firstPublishedOn
138
+ ) ,
139
+ lastPublishedOn : getDate ( rawInput . lastPublishedOn , currentDateTime ) ,
140
+ firstPublishedBy : getIdentity (
141
+ rawInput . firstPublishedBy ,
142
+ latestStorageEntry . firstPublishedBy
143
+ ) ,
144
+ lastPublishedBy : getIdentity ( rawInput . lastPublishedBy , currentIdentity )
145
+ } ;
146
+ }
147
+
77
148
const entry : CmsEntry = {
78
149
...originalEntry ,
79
150
id,
@@ -85,33 +156,24 @@ export const createEntryRevisionFromData = async ({
85
156
createdOn : getDate ( rawInput . createdOn , latestStorageEntry . createdOn ) ,
86
157
savedOn : getDate ( rawInput . savedOn , currentDateTime ) ,
87
158
modifiedOn : getDate ( rawInput . modifiedOn , currentDateTime ) ,
88
- firstPublishedOn : getDate ( rawInput . firstPublishedOn , latestStorageEntry . firstPublishedOn ) ,
89
- lastPublishedOn : getDate ( rawInput . lastPublishedOn , latestStorageEntry . lastPublishedOn ) ,
90
159
createdBy : getIdentity ( rawInput . createdBy , latestStorageEntry . createdBy ) ,
91
160
savedBy : getIdentity ( rawInput . savedBy , currentIdentity ) ,
92
161
modifiedBy : getIdentity ( rawInput . modifiedBy , currentIdentity ) ,
93
- firstPublishedBy : getIdentity (
94
- rawInput . firstPublishedBy ,
95
- latestStorageEntry . firstPublishedBy
96
- ) ,
97
- lastPublishedBy : getIdentity ( rawInput . lastPublishedBy , latestStorageEntry . lastPublishedBy ) ,
162
+ ...entryLevelPublishingMetaFields ,
98
163
99
164
/**
100
165
* Revision-level meta fields. 👇
101
166
*/
102
167
revisionCreatedOn : getDate ( rawInput . revisionCreatedOn , currentDateTime ) ,
103
168
revisionSavedOn : getDate ( rawInput . revisionSavedOn , currentDateTime ) ,
104
169
revisionModifiedOn : getDate ( rawInput . revisionModifiedOn , null ) ,
105
- revisionFirstPublishedOn : getDate ( rawInput . revisionFirstPublishedOn , null ) ,
106
- revisionLastPublishedOn : getDate ( rawInput . revisionLastPublishedOn , null ) ,
107
170
revisionCreatedBy : getIdentity ( rawInput . revisionCreatedBy , currentIdentity ) ,
108
171
revisionSavedBy : getIdentity ( rawInput . revisionSavedBy , currentIdentity ) ,
109
172
revisionModifiedBy : getIdentity ( rawInput . revisionModifiedBy , null ) ,
110
- revisionFirstPublishedBy : getIdentity ( rawInput . revisionFirstPublishedBy , null ) ,
111
- revisionLastPublishedBy : getIdentity ( rawInput . revisionLastPublishedBy , null ) ,
173
+ ...revisionLevelPublishingMetaFields ,
112
174
113
- locked : false ,
114
- status : STATUS_DRAFT ,
175
+ locked,
176
+ status,
115
177
values
116
178
} ;
117
179
0 commit comments