Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/plugin-import-export/src/export/createExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export type Export = {
page?: number
slug: string
sort: Sort
user: string
userCollection: string
where?: Where
}

Expand All @@ -42,7 +40,7 @@ export type CreateExportArgs = {
download?: boolean
input: Export
req: PayloadRequest
user?: TypedUser
user?: null | TypedUser
}

export const createExport = async (args: CreateExportArgs) => {
Expand All @@ -59,15 +57,19 @@ export const createExport = async (args: CreateExportArgs) => {
format,
locale: localeInput,
sort,
user,
page,
limit: incomingLimit,
where,
},
req: { locale: localeArg, payload },
req,
user,
} = args

if (!user) {
throw new APIError('User authentication is required to create exports')
}

if (debug) {
req.payload.logger.debug({
message: 'Starting export process with args:',
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-import-export/src/export/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export const download = async (req: PayloadRequest, debug = false) => {
const { collectionSlug } = body.data || {}

req.payload.logger.info(`Download request received ${collectionSlug}`)
body.data.user = req.user

const res = await createExport({
download: true,
input: { ...body.data, debug },
req,
user: req.user,
})

return res as Response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { Config, TaskConfig, TypedUser } from 'payload'
import type { Config, PayloadRequest, TaskConfig, TypedUser } from 'payload'

import type { ImportExportPluginConfig } from '../types.js'
import type { CreateExportArgs, Export } from './createExport.js'
import type { Export } from './createExport.js'

import { createExport } from './createExport.js'
import { getFields } from './getFields.js'

/**
* Export input type for job queue serialization.
* When exports are queued as jobs, the user must be serialized as an ID string or number
* along with the collection name so it can be rehydrated when the job runs.
*/
export type ExportJobInput = {
user: number | string
userCollection: string
} & Export

export const getCreateCollectionExportTask = (
config: Config,
pluginConfig?: ImportExportPluginConfig,
): TaskConfig<{
input: Export
input: ExportJobInput
output: object
}> => {
const inputSchema = getFields(config, pluginConfig).concat(
Expand All @@ -30,21 +40,26 @@ export const getCreateCollectionExportTask = (

return {
slug: 'createCollectionExport',
handler: async ({ input, req }: CreateExportArgs) => {
handler: async ({ input, req }: { input: ExportJobInput; req: PayloadRequest }) => {
let user: TypedUser | undefined

if (input.userCollection && input.user) {
user = (await req.payload.findByID({
id: input.user,
collection: input.userCollection,
})) as TypedUser

req.user = user
}

if (!user) {
throw new Error('User not found')
}

await createExport({ input, req, user })
// Strip out user and userCollection from input - they're only needed for rehydration
const { user: _userId, userCollection: _userCollection, ...exportInput } = input

await createExport({ input: exportInput, req, user })

return {
output: {},
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-import-export/src/getExportCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const getExportCollection = ({
}
const { user } = req
const debug = pluginConfig.debug
await createExport({ input: { ...args.data, debug, user }, req })
await createExport({ input: { ...args.data, debug }, req, user })
})
} else {
afterChange.push(async ({ doc, operation, req }) => {
Expand All @@ -86,7 +86,7 @@ export const getExportCollection = ({
...doc,
exportsCollection: collection.slug,
user: req?.user?.id || req?.user?.user?.id,
userCollection: 'users',
userCollection: req.payload.config.admin.user,
}
await req.payload.jobs.queue({
input,
Expand Down
Loading