ocr-delete-job.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @node ocr-delete-job
  3. * @name OCR Delete Job
  4. * @category ocr
  5. * @version 1.0.1
  6. * @description Delete an OCR job
  7. * @icon trash-2
  8. */
  9. const configSchema = {
  10. type: 'object',
  11. properties: {
  12. baseUrl: {
  13. type: 'string',
  14. title: 'API Base URL',
  15. default: 'http://localhost:3000',
  16. description: 'Base URL of the OCR API'
  17. },
  18. credentialId: {
  19. type: 'string',
  20. title: 'Credential',
  21. description: 'Bearer or API Key credential for authentication',
  22. dynamicOptions: {
  23. source: 'credentials',
  24. filter: { type: 'api_key' }
  25. }
  26. },
  27. jobId: {
  28. type: 'string',
  29. title: 'Job ID',
  30. description: 'The ID of the job to delete (can be provided from input)'
  31. }
  32. },
  33. required: ['baseUrl']
  34. };
  35. const inputSchema = {
  36. type: 'object',
  37. properties: {
  38. jobId: {
  39. type: 'string',
  40. description: 'Job ID to delete (overrides config)'
  41. }
  42. }
  43. };
  44. const outputSchema = {
  45. type: 'object',
  46. properties: {
  47. jobId: { type: 'string' },
  48. deleted: { type: 'boolean' }
  49. }
  50. };
  51. const outputs = [
  52. { name: 'main', displayName: 'Deleted', type: 'object', color: '#ef4444' }
  53. ];
  54. module.exports = {
  55. configSchema,
  56. inputSchema,
  57. outputSchema,
  58. outputs,
  59. async execute(config, input, context) {
  60. const baseUrl = (config.baseUrl || 'http://localhost:3000').replace(/\/$/, '');
  61. const jobId = input.jobId || config.jobId;
  62. if (!jobId) {
  63. throw new Error('Job ID is required');
  64. }
  65. const headers = {};
  66. if (config.credentialId) {
  67. const auth = smartbotic.credentials.get(config.credentialId);
  68. if (!auth.success) {
  69. throw new Error(`Failed to load credential: ${auth.error}`);
  70. }
  71. headers[auth.headerName] = auth.headerValue;
  72. }
  73. smartbotic.log.info(`OCR Delete Job: ${jobId}`);
  74. const response = smartbotic.http.request({
  75. method: 'DELETE',
  76. url: `${baseUrl}/api/jobs/${encodeURIComponent(jobId)}`,
  77. headers: headers,
  78. timeout: 30000
  79. });
  80. if (response.status === 404) {
  81. throw new Error(`Job not found: ${jobId}`);
  82. }
  83. if (response.status < 200 || response.status >= 300) {
  84. const errorMsg = typeof response.data === 'string' ? response.data : JSON.stringify(response.data);
  85. if (response.status === 401 && !config.credentialId) { throw new Error('Authentication required - please select a credential in node settings'); } throw new Error(`OCR API error (${response.status}): ${errorMsg}`);
  86. }
  87. return {
  88. jobId: jobId,
  89. deleted: true
  90. };
  91. }
  92. };