Skip to content

Commit 5bc5cf9

Browse files
feat: [TOL-2513][SPA-2443] Bulk actions v2 support (#2528)
* feat: add bulk actions v2 support * chore: update payload types * fix: add separate methods for v2 bulk actions * fix: linting issues * test: add new methods tests * test: adjust tests * chore: add v2 wait method * chore: fix lint errors * chore: export types * chore: fix linter errors * test: tweak unpublish test * chore: fix prettier error * fix: linting * refactor: move test helpers to corresponding file * chore: fix lint * chore: linting fix * chore: another lint fix attempt --------- Co-authored-by: Sofia Margariti <sofia.margariti@contentful.com>
1 parent f014b64 commit 5bc5cf9

File tree

9 files changed

+304
-40
lines changed

9 files changed

+304
-40
lines changed

lib/adapters/REST/endpoints/bulk-action.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import type {
55
BulkActionPublishPayload,
66
BulkActionUnpublishPayload,
77
BulkActionValidatePayload,
8+
PublishBulkActionV2Payload,
9+
UnpublishBulkActionV2Payload,
10+
ValidateBulkActionV2Payload,
811
} from '../../../entities/bulk-action'
912
import type { RestEndpoint } from '../types'
1013
import * as raw from './raw'
@@ -54,3 +57,53 @@ export const validate: RestEndpoint<'BulkAction', 'validate'> = (
5457
payload,
5558
)
5659
}
60+
61+
export const getV2: RestEndpoint<'BulkAction', 'getV2'> = (
62+
http: AxiosInstance,
63+
params: GetBulkActionParams,
64+
) => {
65+
return raw.get(
66+
http,
67+
`/spaces/${params.spaceId}/environments/${params.environmentId}/bulk_actions/${params.bulkActionId}`,
68+
)
69+
}
70+
71+
export const publishV2: RestEndpoint<'BulkAction', 'publishV2'> = (
72+
http: AxiosInstance,
73+
params: GetSpaceEnvironmentParams,
74+
payload: PublishBulkActionV2Payload<'add'>,
75+
): Promise<BulkActionProps<PublishBulkActionV2Payload<'add'>>> => {
76+
return raw.post(
77+
http,
78+
`/spaces/${params.spaceId}/environments/${params.environmentId}/bulk_actions`,
79+
payload,
80+
)
81+
}
82+
83+
export const unpublishV2: RestEndpoint<'BulkAction', 'unpublishV2'> = (
84+
http: AxiosInstance,
85+
params: GetSpaceEnvironmentParams,
86+
payload: PublishBulkActionV2Payload<'remove'> | UnpublishBulkActionV2Payload,
87+
): Promise<
88+
BulkActionProps<PublishBulkActionV2Payload<'remove'> | UnpublishBulkActionV2Payload>
89+
> => {
90+
return raw.post(
91+
http,
92+
`/spaces/${params.spaceId}/environments/${params.environmentId}/bulk_actions`,
93+
payload,
94+
)
95+
}
96+
97+
export const validateV2: RestEndpoint<'BulkAction', 'validateV2'> = (
98+
http: AxiosInstance,
99+
params: GetSpaceEnvironmentParams,
100+
payload: ValidateBulkActionV2Payload<'add'> | ValidateBulkActionV2Payload<'remove'>,
101+
): Promise<
102+
BulkActionProps<ValidateBulkActionV2Payload<'add'> | ValidateBulkActionV2Payload<'remove'>>
103+
> => {
104+
return raw.post(
105+
http,
106+
`/spaces/${params.spaceId}/environments/${params.environmentId}/bulk_actions`,
107+
payload,
108+
)
109+
}

lib/common-types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ import type {
4141
BulkActionProps,
4242
BulkActionPublishPayload,
4343
BulkActionUnpublishPayload,
44+
BulkActionV2Payload,
4445
BulkActionValidatePayload,
46+
PublishBulkActionV2Payload,
47+
UnpublishBulkActionV2Payload,
48+
ValidateBulkActionV2Payload,
4549
} from './entities/bulk-action'
4650
import type {
4751
CommentProps,
@@ -543,6 +547,10 @@ type MRInternal<UA extends boolean> = {
543547
(opts: MROpts<'BulkAction', 'publish', UA>): MRReturn<'BulkAction', 'publish'>
544548
(opts: MROpts<'BulkAction', 'unpublish', UA>): MRReturn<'BulkAction', 'unpublish'>
545549
(opts: MROpts<'BulkAction', 'validate', UA>): MRReturn<'BulkAction', 'validate'>
550+
(opts: MROpts<'BulkAction', 'getV2', UA>): MRReturn<'BulkAction', 'getV2'>
551+
(opts: MROpts<'BulkAction', 'publishV2', UA>): MRReturn<'BulkAction', 'publishV2'>
552+
(opts: MROpts<'BulkAction', 'unpublishV2', UA>): MRReturn<'BulkAction', 'unpublishV2'>
553+
(opts: MROpts<'BulkAction', 'validateV2', UA>): MRReturn<'BulkAction', 'validateV2'>
546554

547555
(opts: MROpts<'Comment', 'get', UA>): MRReturn<'Comment', 'get'>
548556
(opts: MROpts<'Comment', 'getMany', UA>): MRReturn<'Comment', 'getMany'>
@@ -1331,6 +1339,27 @@ export type MRActions = {
13311339
payload: BulkActionValidatePayload
13321340
return: BulkActionProps<BulkActionValidatePayload>
13331341
}
1342+
getV2: {
1343+
params: GetBulkActionParams
1344+
return: BulkActionProps<BulkActionV2Payload>
1345+
}
1346+
publishV2: {
1347+
params: GetSpaceEnvironmentParams
1348+
payload: PublishBulkActionV2Payload<'add'>
1349+
return: BulkActionProps<PublishBulkActionV2Payload<'add'>>
1350+
}
1351+
unpublishV2: {
1352+
params: GetSpaceEnvironmentParams
1353+
payload: PublishBulkActionV2Payload<'remove'> | UnpublishBulkActionV2Payload
1354+
return: BulkActionProps<PublishBulkActionV2Payload<'remove'> | UnpublishBulkActionV2Payload>
1355+
}
1356+
validateV2: {
1357+
params: GetSpaceEnvironmentParams
1358+
payload: ValidateBulkActionV2Payload<'add'> | ValidateBulkActionV2Payload<'remove'>
1359+
return: BulkActionProps<
1360+
ValidateBulkActionV2Payload<'add'> | ValidateBulkActionV2Payload<'remove'>
1361+
>
1362+
}
13341363
}
13351364
Comment: {
13361365
get:

lib/entities/bulk-action.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,49 @@ export interface BulkActionPublishPayload extends MakeRequestPayload {
6969
}
7070
}
7171

72+
interface AddFieldsEntity<L extends Link<Entity> | VersionedLink<Entity>> {
73+
entity: L
74+
add?: {
75+
fields: Record<'*', string[]>
76+
}
77+
}
78+
79+
interface RemoveFieldsEntity<L extends Link<Entity> | VersionedLink<Entity>> {
80+
entity: L
81+
remove?: {
82+
fields: Record<'*', string[]>
83+
}
84+
}
85+
type BulkActionEntity<L extends Link<Entity> | VersionedLink<Entity>> = {
86+
entity: L
87+
}
88+
89+
export interface PublishBulkActionV2Payload<PublishActionType extends 'add' | 'remove' = 'add'> {
90+
action: 'publish'
91+
entities: PublishActionType extends 'remove'
92+
? RemoveFieldsEntity<VersionedLink<Entity>>[]
93+
: AddFieldsEntity<VersionedLink<Entity>>[]
94+
}
95+
96+
export interface ValidateBulkActionV2Payload<PublishActionType extends 'add' | 'remove' = 'add'> {
97+
action: 'validate'
98+
entities: PublishActionType extends 'remove'
99+
? RemoveFieldsEntity<Link<Entity>>[]
100+
: AddFieldsEntity<Link<Entity>>[]
101+
}
102+
103+
export interface UnpublishBulkActionV2Payload {
104+
action: 'unpublish'
105+
entities: BulkActionEntity<Link<Entity>>[]
106+
}
107+
108+
export type BulkActionV2Payload =
109+
| PublishBulkActionV2Payload<'add'>
110+
| PublishBulkActionV2Payload<'remove'>
111+
| UnpublishBulkActionV2Payload
112+
| ValidateBulkActionV2Payload<'add'>
113+
| ValidateBulkActionV2Payload<'remove'>
114+
72115
export type BulkActionSysProps = {
73116
id: string
74117
type: 'BulkAction'
@@ -81,7 +124,7 @@ export type BulkActionSysProps = {
81124
}
82125

83126
/** The object returned by the BulkActions API */
84-
export interface BulkActionProps<TPayload extends BulkActionPayload = any> {
127+
export interface BulkActionProps<TPayload extends BulkActionPayload | BulkActionV2Payload = any> {
85128
sys: BulkActionSysProps
86129
action: BulkActionType
87130
/** original payload when BulkAction was created */
@@ -120,15 +163,15 @@ function createBulkActionApi(makeRequest: MakeRequest) {
120163
params,
121164
}).then((bulkAction) => wrapBulkAction(makeRequest, bulkAction))
122165
},
123-
async waitProcessing<TPayload extends BulkActionPayload = any>(
166+
async waitProcessing<TPayload extends BulkActionPayload | BulkActionV2Payload = any>(
124167
options?: AsyncActionProcessingOptions,
125168
): Promise<BulkActionProps<TPayload>> {
126169
return pollAsyncActionStatus<BulkActionProps<TPayload>>(async () => this.get(), options)
127170
},
128171
}
129172
}
130173

131-
export interface BulkAction<T extends BulkActionPayload = any>
174+
export interface BulkAction<T extends BulkActionPayload | BulkActionV2Payload = any>
132175
extends BulkActionProps<T>,
133176
BulkActionApiMethods,
134177
DefaultElements<BulkActionProps<T>> {}
@@ -139,9 +182,9 @@ export interface BulkAction<T extends BulkActionPayload = any>
139182
* @param data - Raw BulkAction data
140183
* @return Wrapped BulkAction data
141184
*/
142-
export function wrapBulkAction<TPayload extends BulkActionPayload = any>(
185+
export function wrapBulkAction<TPayload extends BulkActionPayload | BulkActionV2Payload = any>(
143186
makeRequest: MakeRequest,
144-
data: BulkActionProps<BulkActionPayload>,
187+
data: BulkActionProps<BulkActionPayload | BulkActionV2Payload>,
145188
): BulkAction<TPayload> {
146189
const bulkAction = toPlainObject(copy(data))
147190
const bulkActionWithApiMethods = enhanceWithMethods(

lib/export-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export type {
8585
BulkActionType,
8686
BulkActionUnpublishPayload,
8787
BulkActionValidatePayload,
88+
BulkActionV2Payload,
89+
PublishBulkActionV2Payload,
90+
UnpublishBulkActionV2Payload,
91+
ValidateBulkActionV2Payload,
8892
} from './entities/bulk-action'
8993
export type {
9094
Comment,

lib/methods/bulk-action.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/plain/common-types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ import type {
4949
BulkActionProps,
5050
BulkActionPublishPayload,
5151
BulkActionUnpublishPayload,
52+
BulkActionV2Payload,
5253
BulkActionValidatePayload,
54+
PublishBulkActionV2Payload,
55+
UnpublishBulkActionV2Payload,
56+
ValidateBulkActionV2Payload,
5357
} from '../entities/bulk-action'
5458
import type { ContentTypeProps, CreateContentTypeProps } from '../entities/content-type'
5559
import type { CreateEntryProps, EntryProps, EntryReferenceProps } from '../entities/entry'
@@ -247,6 +251,21 @@ export type PlainClientAPI = {
247251
params: GetSpaceEnvironmentParams,
248252
payload: BulkActionValidatePayload,
249253
): Promise<BulkActionProps<BulkActionValidatePayload>>
254+
getV2(params: GetBulkActionParams): Promise<BulkActionProps<BulkActionV2Payload>>
255+
publishV2(
256+
params: GetSpaceEnvironmentParams,
257+
payload: PublishBulkActionV2Payload<'add'>,
258+
): Promise<BulkActionProps<PublishBulkActionV2Payload<'add'>>>
259+
unpublishV2(
260+
params: GetSpaceEnvironmentParams,
261+
payload: PublishBulkActionV2Payload<'remove'> | UnpublishBulkActionV2Payload,
262+
): Promise<BulkActionProps<PublishBulkActionV2Payload<'remove'> | UnpublishBulkActionV2Payload>>
263+
validateV2(
264+
params: GetSpaceEnvironmentParams,
265+
payload: ValidateBulkActionV2Payload<'add'> | ValidateBulkActionV2Payload<'remove'>,
266+
): Promise<
267+
BulkActionProps<ValidateBulkActionV2Payload<'add'> | ValidateBulkActionV2Payload<'remove'>>
268+
>
250269
}
251270
comment: CommentPlainClientAPI
252271
concept: ConceptPlainClientAPI

lib/plain/plain-client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ export const createPlainClient = (
202202
publish: wrap(wrapParams, 'BulkAction', 'publish'),
203203
unpublish: wrap(wrapParams, 'BulkAction', 'unpublish'),
204204
validate: wrap(wrapParams, 'BulkAction', 'validate'),
205+
getV2: wrap(wrapParams, 'BulkAction', 'getV2'),
206+
publishV2: wrap(wrapParams, 'BulkAction', 'publishV2'),
207+
unpublishV2: wrap(wrapParams, 'BulkAction', 'unpublishV2'),
208+
validateV2: wrap(wrapParams, 'BulkAction', 'validateV2'),
205209
},
206210
comment: {
207211
get: wrap(wrapParams, 'Comment', 'get') as PlainClientAPI['comment']['get'],

test/helpers.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import type { CreateHttpClientParams } from 'contentful-sdk-core'
2+
import * as testUtils from '@contentful/integration-test-utils'
23
import { createClient } from '../lib/contentful-management'
3-
import type { Environment, Organization, Space } from '../lib/contentful-management'
4+
import type {
5+
BulkActionPayload,
6+
BulkActionProps,
7+
BulkActionV2Payload,
8+
Environment,
9+
Organization,
10+
PlainClientAPI,
11+
Space,
12+
} from '../lib/contentful-management'
413
import { TestDefaults } from './defaults'
14+
import { AsyncActionProcessingOptions, pollAsyncActionStatus } from '../lib/methods/action'
515

6-
import * as testUtils from '@contentful/integration-test-utils'
16+
type PlainOptions = {
17+
/** Used by the PlainClient to perform a poll for the BulkAction status */
18+
plainClient: PlainClientAPI
19+
spaceId: string
20+
environmentId: string
21+
bulkActionId: string
22+
}
723

824
const accessToken = process.env.CONTENTFUL_INTEGRATION_TEST_CMA_TOKEN
925
const orgId = process.env.CONTENTFUL_ORGANIZATION_ID
@@ -200,3 +216,37 @@ export const cleanupTaxonomy = async (olderThan = 1000 * 60 * 60) => {
200216
}
201217

202218
export const timeoutToCalmRateLimiting = () => new Promise((resolve) => setTimeout(resolve, 1000))
219+
220+
/** Waits for a BulkAction status to be either succeeded or failed.
221+
* Used by the Plain client */
222+
export async function waitForBulkActionProcessing<T extends BulkActionPayload = any>(
223+
{ plainClient, spaceId, environmentId, bulkActionId }: PlainOptions,
224+
options?: AsyncActionProcessingOptions,
225+
): Promise<BulkActionProps<T>> {
226+
return pollAsyncActionStatus<BulkActionProps>(
227+
async () =>
228+
plainClient.bulkAction.get<T>({
229+
bulkActionId,
230+
spaceId,
231+
environmentId,
232+
}),
233+
options,
234+
)
235+
}
236+
237+
/** Waits for a BulkAction V2 status to be either succeeded or failed.
238+
* Used by the Plain client */
239+
export async function waitForBulkActionV2Processing<T extends BulkActionV2Payload = any>(
240+
{ plainClient, spaceId, environmentId, bulkActionId }: PlainOptions,
241+
options?: AsyncActionProcessingOptions,
242+
): Promise<BulkActionProps<T>> {
243+
return pollAsyncActionStatus<BulkActionProps>(
244+
async () =>
245+
plainClient.bulkAction.getV2({
246+
bulkActionId,
247+
spaceId,
248+
environmentId,
249+
}),
250+
options,
251+
)
252+
}

0 commit comments

Comments
 (0)