|
| 1 | +import { IExecuteFunctions, IDataObject, NodeOperationError } from 'n8n-workflow'; |
| 2 | +import { EntityHandler } from './EntityHandler'; |
| 3 | +import { espoApiRequest, espoApiRequestAllItems } from '../GenericFunctions'; |
| 4 | +import { toEspoDate, toEspoDateTime } from './Utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Class for handling Call entity operations |
| 8 | + */ |
| 9 | +export class CallHandler implements EntityHandler { |
| 10 | + async create(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 11 | + const entityData: IDataObject = {}; |
| 12 | + entityData.name = this.getNodeParameter('name', index) as string; |
| 13 | + entityData.dateStart = this.getNodeParameter('dateStart', index) as string; |
| 14 | + entityData.dateEnd = this.getNodeParameter('dateEnd', index) as string; |
| 15 | + |
| 16 | + const additionalFields = this.getNodeParameter('additionalFields', index, {}) as IDataObject; |
| 17 | + Object.assign(entityData, additionalFields); |
| 18 | + |
| 19 | + // Normalize date fields |
| 20 | + if (entityData.dateStart) { |
| 21 | + const ds = toEspoDateTime(entityData.dateStart as string); |
| 22 | + entityData.dateStart = ds; |
| 23 | + entityData.dateStartDate = toEspoDate(ds); |
| 24 | + } |
| 25 | + if (entityData.dateEnd) { |
| 26 | + const de = toEspoDateTime(entityData.dateEnd as string); |
| 27 | + entityData.dateEnd = de; |
| 28 | + entityData.dateEndDate = toEspoDate(de); |
| 29 | + } |
| 30 | + |
| 31 | + const endpoint = '/call'; |
| 32 | + const responseData = await espoApiRequest.call(this, 'POST', endpoint, entityData); |
| 33 | + return responseData as IDataObject; |
| 34 | + } |
| 35 | + |
| 36 | + async get(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 37 | + const id = this.getNodeParameter('callId', index) as string; |
| 38 | + const endpoint = `/call/${id}`; |
| 39 | + const responseData = await espoApiRequest.call(this, 'GET', endpoint); |
| 40 | + return responseData as IDataObject; |
| 41 | + } |
| 42 | + |
| 43 | + async update(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 44 | + const id = this.getNodeParameter('callId', index) as string; |
| 45 | + const updateFields = this.getNodeParameter('updateFields', index, {}) as IDataObject; |
| 46 | + if (updateFields.dateStart) { |
| 47 | + const ds = toEspoDateTime(updateFields.dateStart as string); |
| 48 | + updateFields.dateStart = ds; |
| 49 | + updateFields.dateStartDate = toEspoDate(ds); |
| 50 | + } |
| 51 | + if (updateFields.dateEnd) { |
| 52 | + const de = toEspoDateTime(updateFields.dateEnd as string); |
| 53 | + updateFields.dateEnd = de; |
| 54 | + updateFields.dateEndDate = toEspoDate(de); |
| 55 | + } |
| 56 | + const endpoint = `/call/${id}`; |
| 57 | + const responseData = await espoApiRequest.call(this, 'PATCH', endpoint, updateFields); |
| 58 | + return responseData as IDataObject; |
| 59 | + } |
| 60 | + |
| 61 | + async delete(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 62 | + const id = this.getNodeParameter('callId', index) as string; |
| 63 | + const endpoint = `/call/${id}`; |
| 64 | + await espoApiRequest.call(this, 'DELETE', endpoint); |
| 65 | + return { success: true, entityType: 'call', id, message: 'Call deleted successfully' }; |
| 66 | + } |
| 67 | + |
| 68 | + async getAll(this: IExecuteFunctions, index: number): Promise<IDataObject[]> { |
| 69 | + const returnAll = this.getNodeParameter('returnAll', index) as boolean; |
| 70 | + const endpoint = '/call'; |
| 71 | + const qs: IDataObject = {}; |
| 72 | + |
| 73 | + const filterOptions = this.getNodeParameter('filterOptions', index, {}) as IDataObject; |
| 74 | + |
| 75 | + if (filterOptions.where) { |
| 76 | + if (typeof filterOptions.where === 'string') { |
| 77 | + try { |
| 78 | + qs.where = JSON.parse(filterOptions.where); |
| 79 | + } catch (e: any) { |
| 80 | + throw new NodeOperationError(this.getNode(), `Invalid JSON in 'where' parameter: ${e.message}`); |
| 81 | + } |
| 82 | + } else { |
| 83 | + qs.where = filterOptions.where; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (filterOptions.orderBy) qs.orderBy = filterOptions.orderBy; |
| 88 | + if (filterOptions.order) qs.order = filterOptions.order; |
| 89 | + if (filterOptions.select) qs.select = filterOptions.select; |
| 90 | + if (filterOptions.offset) qs.offset = filterOptions.offset; |
| 91 | + if (filterOptions.boolFilterList) qs.boolFilterList = filterOptions.boolFilterList; |
| 92 | + if (filterOptions.primaryFilter) qs.primaryFilter = filterOptions.primaryFilter; |
| 93 | + |
| 94 | + const headers: IDataObject = {}; |
| 95 | + if (filterOptions.skipTotalCount === true) headers['X-No-Total'] = 'true'; |
| 96 | + |
| 97 | + if (returnAll === true) { |
| 98 | + return await espoApiRequestAllItems.call(this, 'GET', endpoint, {}, qs); |
| 99 | + } else { |
| 100 | + const limit = this.getNodeParameter('limit', index) as number; |
| 101 | + qs.maxSize = limit; |
| 102 | + const response = await espoApiRequest.call(this, 'GET', endpoint, {}, qs, undefined, headers); |
| 103 | + return response.list as IDataObject[]; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments