2024-08-07 02:18:05 -07:00
import type {
IDataObject ,
INodeExecutionData ,
INodeProperties ,
IExecuteFunctions ,
} from 'n8n-workflow' ;
import { updateDisplayOptions , wrapData } from '../../../../../utils/utilities' ;
import { webflowApiRequest } from '../../../GenericFunctions' ;
const properties : INodeProperties [ ] = [
{
displayName : 'Site Name or ID' ,
name : 'siteId' ,
type : 'options' ,
required : true ,
typeOptions : {
loadOptionsMethod : 'getSites' ,
} ,
default : '' ,
description :
2024-09-12 07:53:36 -07:00
'ID of the site containing the collection whose items to add to. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.' ,
2024-08-07 02:18:05 -07:00
} ,
{
displayName : 'Collection Name or ID' ,
name : 'collectionId' ,
type : 'options' ,
required : true ,
typeOptions : {
loadOptionsMethod : 'getCollections' ,
loadOptionsDependsOn : [ 'siteId' ] ,
} ,
default : '' ,
description :
2024-09-12 07:53:36 -07:00
'ID of the collection to add an item to. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.' ,
2024-08-07 02:18:05 -07:00
} ,
{
displayName : 'Item ID' ,
name : 'itemId' ,
type : 'string' ,
required : true ,
default : '' ,
description : 'ID of the item to update' ,
} ,
{
displayName : 'Live' ,
name : 'live' ,
type : 'boolean' ,
required : true ,
default : false ,
description : 'Whether the item should be published on the live site' ,
} ,
{
displayName : 'Fields' ,
name : 'fieldsUi' ,
placeholder : 'Add Field' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : true ,
} ,
default : { } ,
options : [
{
displayName : 'Field' ,
name : 'fieldValues' ,
values : [
{
displayName : 'Field Name or ID' ,
name : 'fieldId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getFields' ,
loadOptionsDependsOn : [ 'collectionId' ] ,
} ,
default : '' ,
description :
2024-09-12 07:53:36 -07:00
'Field to set for the item to create. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.' ,
2024-08-07 02:18:05 -07:00
} ,
{
displayName : 'Field Value' ,
name : 'fieldValue' ,
type : 'string' ,
default : '' ,
description : 'Value to set for the item to create' ,
} ,
] ,
} ,
] ,
} ,
] ;
const displayOptions = {
show : {
resource : [ 'item' ] ,
operation : [ 'update' ] ,
} ,
} ;
export const description = updateDisplayOptions ( displayOptions , properties ) ;
export async function execute (
this : IExecuteFunctions ,
items : INodeExecutionData [ ] ,
) : Promise < INodeExecutionData [ ] > {
const returnData : INodeExecutionData [ ] = [ ] ;
let responseData ;
for ( let i = 0 ; i < items . length ; i ++ ) {
try {
const collectionId = this . getNodeParameter ( 'collectionId' , i ) as string ;
const itemId = this . getNodeParameter ( 'itemId' , i ) as string ;
const uiFields = this . getNodeParameter ( 'fieldsUi.fieldValues' , i , [ ] ) as IDataObject [ ] ;
const live = this . getNodeParameter ( 'live' , i ) as boolean ;
const fieldData = { } as IDataObject ;
uiFields . forEach ( ( data ) = > ( fieldData [ data . fieldId as string ] = data . fieldValue ) ) ;
const body : IDataObject = {
fieldData ,
} ;
responseData = await webflowApiRequest . call (
this ,
'PATCH' ,
` /collections/ ${ collectionId } /items/ ${ itemId } ` ,
body ,
{ live } ,
) ;
const executionData = this . helpers . constructExecutionMetaData (
wrapData ( responseData . body as IDataObject [ ] ) ,
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
returnData . push ( { json : { message : error.message , error } } ) ;
continue ;
}
throw error ;
}
}
return returnData ;
}