kaoqin-services
Version:
考勤系统服务库 (重构版 - 支持n8n webhook)
1 lines • 3.27 kB
JavaScript
;const BaseNotificationService=require("./base-notification");class CompositeNotificationService extends BaseNotificationService{constructor(a=[]){super(),this.services=a||[]}addService(a){return a instanceof BaseNotificationService?this.services.push(a):console.warn("\u5C1D\u8BD5\u6DFB\u52A0\u975E\u901A\u77E5\u670D\u52A1\u5B9E\u4F8B:",a),this}removeService(a){const b=this.services.indexOf(a);return-1!==b&&this.services.splice(b,1),this}getServices(){return[...this.services]}async sendNotification(a,b,c={}){const d=[],e=[];for(const f of this.services)try{const e=await f.sendNotification(a,b,c);d.push({service:f.constructor.name,success:!0,result:e})}catch(a){console.error(`通过 ${f.constructor.name} 发送通知失败:`,a.message),e.push({service:f.constructor.name,error:a.message}),d.push({service:f.constructor.name,success:!1,error:a.message})}if(e.length===this.services.length&&0<this.services.length)throw new Error(`所有通知服务发送失败: ${JSON.stringify(e)}`);return d}async sendKaoqinNotification(a,b,c,d,e={}){const f=[],g=[];for(const h of this.services)try{const g=await h.sendKaoqinNotification(a,b,c,d,e);f.push({service:h.constructor.name,success:!0,result:g})}catch(a){console.error(`通过 ${h.constructor.name} 发送打卡提醒失败:`,a.message),g.push({service:h.constructor.name,error:a.message}),f.push({service:h.constructor.name,success:!1,error:a.message})}if(g.length===this.services.length&&0<this.services.length)throw new Error(`所有通知服务发送打卡提醒失败: ${JSON.stringify(g)}`);return f}async sendDetailedRecords(a,b,c={}){const d=[],e=[];for(const f of this.services)try{const e=await f.sendDetailedRecords(a,b,c);d.push({service:f.constructor.name,success:!0,result:e})}catch(a){console.error(`通过 ${f.constructor.name} 发送详细打卡记录失败:`,a.message),e.push({service:f.constructor.name,error:a.message}),d.push({service:f.constructor.name,success:!1,error:a.message})}if(e.length===this.services.length&&0<this.services.length)throw new Error(`所有通知服务发送详细打卡记录失败: ${JSON.stringify(e)}`);return d}async sendErrorNotification(a,b,c={}){const d=[],e=[];for(const f of this.services)try{const e=await f.sendErrorNotification(a,b,c);d.push({service:f.constructor.name,success:!0,result:e})}catch(a){console.error(`通过 ${f.constructor.name} 发送错误通知失败:`,a.message),e.push({service:f.constructor.name,error:a.message}),d.push({service:f.constructor.name,success:!1,error:a.message})}if(e.length===this.services.length&&0<this.services.length)throw new Error(`所有通知服务发送错误通知失败: ${JSON.stringify(e)}`);return d}async sendHolidayGreeting(a,b,c,d={}){const e=[],f=[];for(const g of this.services)try{const f=await g.sendHolidayGreeting(a,b,c,d);e.push({service:g.constructor.name,success:!0,result:f})}catch(a){console.error(`通过 ${g.constructor.name} 发送节日问候失败:`,a.message),f.push({service:g.constructor.name,error:a.message}),e.push({service:g.constructor.name,success:!1,error:a.message})}if(f.length===this.services.length&&0<this.services.length)throw new Error(`所有通知服务发送节日问候失败: ${JSON.stringify(f)}`);return e}}module.exports=CompositeNotificationService;