rez_core 5.0.29 → 5.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +26 -0
- package/.idea/250218_nodejs_core.iml +9 -0
- package/.idea/codeStyles/Project.xml +59 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/copilot.data.migration.agent.xml +6 -0
- package/.idea/copilot.data.migration.ask.xml +6 -0
- package/.idea/copilot.data.migration.ask2agent.xml +6 -0
- package/.idea/copilot.data.migration.edit.xml +6 -0
- package/.idea/inspectionProfiles/Project_Default.xml +6 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/prettier.xml +6 -0
- package/.idea/vcs.xml +6 -0
- package/.prettierrc +3 -3
- package/README.md +99 -99
- package/dist/module/auth/guards/role.guard.js +3 -3
- package/dist/module/auth/services/auth.service.js +2 -2
- package/dist/module/filter/repository/saved-filter.repository.js +4 -4
- package/dist/module/filter/service/filter-evaluator.service.js +2 -2
- package/dist/module/filter/service/filter.service.js +22 -22
- package/dist/module/integration/examples/usage.example.js +9 -9
- package/dist/module/integration/service/integration.service.js +1 -1
- package/dist/module/integration/service/wrapper.service.js +26 -26
- package/dist/module/listmaster/service/list-master-item.service.js +2 -2
- package/dist/module/listmaster/service/list-master.service.js +0 -9
- package/dist/module/listmaster/service/list-master.service.js.map +1 -1
- package/dist/module/mapper/service/field-mapper.service.js +4 -4
- package/dist/module/mapper/service/mapper.service.js +2 -2
- package/dist/module/meta/repository/entity-attribute-update.repository.js +3 -3
- package/dist/module/meta/repository/entity-master.repository.js +6 -6
- package/dist/module/meta/service/entity-dynamic.service.js +44 -44
- package/dist/module/meta/service/entity-list.service.js +3 -3
- package/dist/module/meta/service/entity-master.service.js +3 -3
- package/dist/module/meta/service/entity-relation.service.js +9 -9
- package/dist/module/meta/service/entity-service-impl.service.js +3 -3
- package/dist/module/meta/service/resolver.service.js +3 -3
- package/dist/module/module/repository/menu.repository.js +12 -12
- package/dist/module/notification/service/notification.service.js +10 -10
- package/dist/module/user/controller/login.controller.js +18 -18
- package/dist/module/user/service/role.service.js +4 -4
- package/dist/module/user/service/user-session.service.js +2 -2
- package/dist/module/workflow/repository/action.repository.js +21 -20
- package/dist/module/workflow/repository/action.repository.js.map +1 -1
- package/dist/module/workflow/repository/comm-template.repository.js +6 -6
- package/dist/module/workflow/repository/form-master.repository.js +2 -2
- package/dist/module/workflow/repository/stage-group.repository.js +20 -20
- package/dist/module/workflow/repository/stage-movement.repository.js +17 -17
- package/dist/module/workflow/repository/stage.repository.js +8 -8
- package/dist/module/workflow/service/action-template-mapping.service.js +31 -31
- package/dist/module/workflow/service/action.service.js +7 -7
- package/dist/module/workflow/service/entity-modification.service.js +2 -2
- package/dist/module/workflow/service/stage-group.service.js +2 -2
- package/dist/module/workflow/service/stage.service.js +2 -2
- package/dist/module/workflow/service/task.service.js +35 -35
- package/dist/module/workflow/service/workflow-list-master.service.js +15 -15
- package/dist/module/workflow/service/workflow-meta.service.js +26 -26
- package/dist/module/workflow/service/workflow.service.js +2 -2
- package/dist/module/workflow-automation/service/schedule-handler.service.js +9 -9
- package/dist/module/workflow-schedule/processors/schedule.processor.js +4 -4
- package/dist/module/workflow-schedule/service/workflow-schedule.service.js +9 -9
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/utils/service/reflection-helper.service.js +2 -2
- package/docs/modules/event-driven-integration-design.md +91 -91
- package/docs/modules/integration.md +250 -250
- package/eslint.config.mjs +34 -34
- package/nest-cli.json +14 -14
- package/package.json +124 -124
- package/server.log +850 -0
- package/src/app.controller.ts +12 -12
- package/src/app.module.ts +66 -66
- package/src/app.service.ts +8 -8
- package/src/config/bull.config.ts +69 -69
- package/src/config/config.module.ts +17 -17
- package/src/config/database.config.ts +48 -48
- package/src/constant/global.constant.ts +67 -67
- package/src/core.module.ts +88 -88
- package/src/decorators/roles.decorator.ts +7 -7
- package/src/dtos/response.dto.ts +6 -6
- package/src/dtos/response.ts +5 -5
- package/src/index.ts +1 -1
- package/src/module/auth/auth.module.ts +49 -49
- package/src/module/auth/controller/auth.controller.ts +28 -28
- package/src/module/auth/guards/google-auth.guard.ts +9 -9
- package/src/module/auth/guards/jwt.guard.ts +22 -22
- package/src/module/auth/guards/role.guard.ts +68 -68
- package/src/module/auth/services/auth.service.ts +50 -50
- package/src/module/auth/services/jwt.service.ts +11 -11
- package/src/module/auth/strategies/google.strategy.ts +54 -54
- package/src/module/auth/strategies/jwt.strategy.ts +58 -58
- package/src/module/auth/strategies/local.strategy.ts +13 -13
- package/src/module/dashboard/controller/dashboard.controller.ts +36 -36
- package/src/module/dashboard/dashboard.module.ts +21 -21
- package/src/module/dashboard/entity/dashboard_page_data.entity.ts +27 -27
- package/src/module/dashboard/entity/widget_master.entity.ts +18 -18
- package/src/module/dashboard/repository/dashboard.repository.ts +42 -42
- package/src/module/dashboard/service/dashboard.service.ts +73 -73
- package/src/module/dev/dev.module.ts +12 -12
- package/src/module/dev/service/dev.service.ts +7 -7
- package/src/module/enterprise/controller/organization.controller.ts +36 -36
- package/src/module/enterprise/enterprise.module.ts +30 -30
- package/src/module/enterprise/entity/enterprise.entity.ts +37 -37
- package/src/module/enterprise/entity/organization-app-mapping.entity.ts +13 -13
- package/src/module/enterprise/entity/organization.entity.ts +92 -92
- package/src/module/enterprise/repository/enterprise.repository.ts +31 -31
- package/src/module/enterprise/repository/organization.repository.ts +26 -26
- package/src/module/enterprise/repository/school.repository.ts +278 -278
- package/src/module/enterprise/service/brand.service.ts +5 -5
- package/src/module/enterprise/service/enterprise.service.ts +16 -16
- package/src/module/enterprise/service/organization-app-mapping.service.ts +4 -4
- package/src/module/enterprise/service/organization.service.ts +145 -145
- package/src/module/entity_json/controller/entity_json.controller.ts +47 -47
- package/src/module/entity_json/entity/entityJson.entity.ts +39 -39
- package/src/module/entity_json/entity_json.module.ts +18 -18
- package/src/module/entity_json/service/entityJson.repository.ts +37 -37
- package/src/module/entity_json/service/entity_json.service.ts +234 -234
- package/src/module/filter/controller/filter.controller.ts +84 -84
- package/src/module/filter/dto/filter-request.dto.ts +38 -38
- package/src/module/filter/entity/saved-filter-detail.entity.ts +41 -41
- package/src/module/filter/entity/saved-filter-master.entity.ts +23 -23
- package/src/module/filter/filter.module.ts +31 -31
- package/src/module/filter/repository/saved-filter.repository.ts +168 -168
- package/src/module/filter/service/filter-evaluator.service.ts +86 -86
- package/src/module/filter/service/filter.service.ts +1144 -1144
- package/src/module/filter/service/saved-filter.service.ts +170 -170
- package/src/module/ics/controller/ics.controller.ts +21 -21
- package/src/module/ics/dto/ics.dto.ts +55 -55
- package/src/module/ics/ics.module.ts +13 -13
- package/src/module/ics/service/ics.service.ts +57 -57
- package/src/module/integration/controller/calender-event.controller.ts +31 -31
- package/src/module/integration/controller/integration.controller.ts +662 -662
- package/src/module/integration/controller/wrapper.controller.ts +37 -37
- package/src/module/integration/dto/create-config.dto.ts +526 -526
- package/src/module/integration/entity/integration-config.entity.ts +112 -112
- package/src/module/integration/entity/integration-entity-mapper.entity.ts +14 -14
- package/src/module/integration/entity/integration-source.entity.ts +17 -17
- package/src/module/integration/entity/user-integration.entity.ts +71 -71
- package/src/module/integration/examples/usage.example.ts +338 -338
- package/src/module/integration/factories/base.factory.ts +7 -7
- package/src/module/integration/factories/email.factory.ts +49 -49
- package/src/module/integration/factories/integration.factory.ts +121 -121
- package/src/module/integration/factories/sms.factory.ts +51 -51
- package/src/module/integration/factories/telephone.factory.ts +41 -41
- package/src/module/integration/factories/whatsapp.factory.ts +56 -56
- package/src/module/integration/integration.module.ts +110 -110
- package/src/module/integration/service/calendar-event.service.ts +118 -118
- package/src/module/integration/service/integration-entity-mapper.service.ts +17 -17
- package/src/module/integration/service/integration-queue.service.ts +229 -229
- package/src/module/integration/service/integration.service.ts +2633 -2633
- package/src/module/integration/service/oauth.service.ts +224 -224
- package/src/module/integration/service/wrapper.service.ts +701 -701
- package/src/module/integration/strategies/email/gmail-api.strategy.ts +280 -280
- package/src/module/integration/strategies/email/outlook-api.strategy.ts +44 -44
- package/src/module/integration/strategies/email/outlook.strategy.ts +64 -64
- package/src/module/integration/strategies/email/sendgrid-api.strategy.ts +260 -260
- package/src/module/integration/strategies/integration.strategy.ts +97 -97
- package/src/module/integration/strategies/sms/gupshup-sms.strategy.ts +146 -146
- package/src/module/integration/strategies/sms/msg91-sms.strategy.ts +164 -164
- package/src/module/integration/strategies/sms/tubelight-sms.strategy.ts +163 -163
- package/src/module/integration/strategies/telephone/ozonetel-voice.strategy.ts +238 -238
- package/src/module/integration/strategies/telephone/tubelight-voice.strategy.ts +210 -210
- package/src/module/integration/strategies/whatsapp/gupshup-whatsapp.strategy.ts +359 -359
- package/src/module/integration/strategies/whatsapp/tubelight-whatsapp.strategy.ts +372 -372
- package/src/module/integration/strategies/whatsapp/whatsapp-cloud.strategy.ts +403 -403
- package/src/module/integration/strategies/whatsapp/whatsapp.strategy.ts +57 -57
- package/src/module/layout/controller/layout.controller.ts +47 -47
- package/src/module/layout/entity/header-items.entity.ts +28 -28
- package/src/module/layout/entity/header-section.entity.ts +19 -19
- package/src/module/layout/layout.module.ts +21 -21
- package/src/module/layout/repository/header-items.repository.ts +18 -18
- package/src/module/layout/repository/header-section.repository.ts +22 -22
- package/src/module/layout/service/header-section.service.ts +25 -25
- package/src/module/layout_preference/controller/layout_preference.controller.ts +73 -73
- package/src/module/layout_preference/entity/layout_preference.entity.ts +28 -28
- package/src/module/layout_preference/layout_preference.module.ts +22 -22
- package/src/module/layout_preference/repository/layout_preference.repository.ts +65 -65
- package/src/module/layout_preference/service/layout_preference.service.ts +191 -191
- package/src/module/lead/controller/lead.controller.ts +30 -30
- package/src/module/lead/lead.module.ts +14 -14
- package/src/module/lead/repository/lead.repository.ts +41 -41
- package/src/module/lead/service/lead.service.ts +54 -54
- package/src/module/linked_attributes/controller/linked_attributes.controller.ts +34 -34
- package/src/module/linked_attributes/entity/linked_attribute.entity.ts +51 -51
- package/src/module/linked_attributes/linked_attributes.module.ts +16 -16
- package/src/module/linked_attributes/repository/linked_attribute.repository.ts +12 -12
- package/src/module/linked_attributes/service/linked_attributes.service.ts +102 -102
- package/src/module/listmaster/controller/list-master.controller.ts +230 -230
- package/src/module/listmaster/entity/list-master-items.entity.ts +43 -43
- package/src/module/listmaster/entity/list-master.entity.ts +33 -33
- package/src/module/listmaster/listmaster.module.ts +46 -46
- package/src/module/listmaster/repository/list-master-items.repository.ts +173 -173
- package/src/module/listmaster/repository/list-master.repository.ts +46 -46
- package/src/module/listmaster/service/list-master-engine.ts +19 -19
- package/src/module/listmaster/service/list-master-extension.interface.ts +4 -4
- package/src/module/listmaster/service/list-master-item.service.ts +292 -292
- package/src/module/listmaster/service/list-master-registry.ts +15 -15
- package/src/module/listmaster/service/list-master.service.ts +544 -558
- package/src/module/mapper/controller/field-mapper.controller.ts +76 -76
- package/src/module/mapper/controller/mapper.controller.ts +20 -20
- package/src/module/mapper/dto/field-mapper.dto.ts +14 -14
- package/src/module/mapper/entity/field-lovs.entity.ts +19 -19
- package/src/module/mapper/entity/field-mapper.entity.ts +53 -53
- package/src/module/mapper/entity/mapper.entity.ts +16 -16
- package/src/module/mapper/mapper.module.ts +34 -34
- package/src/module/mapper/repository/field-lovs.repository.ts +35 -35
- package/src/module/mapper/repository/field-mapper.repository.ts +42 -42
- package/src/module/mapper/repository/mapper.repository.ts +15 -15
- package/src/module/mapper/service/field-mapper.service.ts +271 -271
- package/src/module/mapper/service/mapper.service.ts +79 -79
- package/src/module/master/controller/master.controller.ts +74 -74
- package/src/module/master/service/master.service.ts +483 -483
- package/src/module/meta/controller/app-master.controller.ts +38 -38
- package/src/module/meta/controller/attribute-master.controller.ts +84 -84
- package/src/module/meta/controller/entity-dynamic.controller.ts +125 -125
- package/src/module/meta/controller/entity-master.controller.ts +41 -41
- package/src/module/meta/controller/entity-relation.controller.ts +36 -36
- package/src/module/meta/controller/entity.controller.ts +392 -392
- package/src/module/meta/controller/entity.public.controller.ts +75 -75
- package/src/module/meta/controller/media.controller.ts +107 -107
- package/src/module/meta/controller/meta.controller.ts +96 -96
- package/src/module/meta/controller/view-master.controller.ts +86 -86
- package/src/module/meta/dto/entity-list-data.dto.ts +6 -6
- package/src/module/meta/dto/entity-tab.dto.ts +4 -4
- package/src/module/meta/dto/entity-table.dto.ts +9 -9
- package/src/module/meta/entity/app-master.entity.ts +34 -34
- package/src/module/meta/entity/attribute-master.entity.ts +92 -92
- package/src/module/meta/entity/base-entity.entity.ts +75 -75
- package/src/module/meta/entity/entity-master.entity.ts +85 -85
- package/src/module/meta/entity/entity-relation-data.entity.ts +29 -29
- package/src/module/meta/entity/entity-relation.entity.ts +23 -23
- package/src/module/meta/entity/entity-table-column.entity.ts +61 -61
- package/src/module/meta/entity/entity-table.entity.ts +50 -50
- package/src/module/meta/entity/media-data.entity.ts +32 -32
- package/src/module/meta/entity/preference.entity.ts +62 -62
- package/src/module/meta/entity/view-master.entity.ts +41 -41
- package/src/module/meta/entity.module.ts +166 -166
- package/src/module/meta/repository/app-master.repository.ts +20 -20
- package/src/module/meta/repository/attribute-master.repository.ts +138 -138
- package/src/module/meta/repository/entity-attribute-update.repository.ts +44 -44
- package/src/module/meta/repository/entity-master.repository.ts +111 -111
- package/src/module/meta/repository/entity-table-column.repository.ts +39 -39
- package/src/module/meta/repository/entity-table.repository.ts +53 -53
- package/src/module/meta/repository/media-data.repository.ts +50 -50
- package/src/module/meta/repository/preference.repository.ts +20 -20
- package/src/module/meta/repository/user-app-mapping.repository.ts +28 -28
- package/src/module/meta/repository/view-master.repository.ts +42 -42
- package/src/module/meta/service/app-master.service.ts +37 -37
- package/src/module/meta/service/attribute-master.service.ts +130 -130
- package/src/module/meta/service/common.service.ts +9 -9
- package/src/module/meta/service/entity-attribute-update.service.ts +28 -28
- package/src/module/meta/service/entity-dynamic.service.ts +809 -809
- package/src/module/meta/service/entity-list.service.ts +205 -205
- package/src/module/meta/service/entity-master.service.ts +175 -175
- package/src/module/meta/service/entity-realation-data.service.ts +9 -9
- package/src/module/meta/service/entity-relation.service.ts +87 -87
- package/src/module/meta/service/entity-service-impl.service.ts +525 -525
- package/src/module/meta/service/entity-table-column.service.ts +39 -39
- package/src/module/meta/service/entity-table.service.ts +150 -150
- package/src/module/meta/service/entity-validation.service.ts +187 -187
- package/src/module/meta/service/entity.service.ts +67 -67
- package/src/module/meta/service/field-group.service.ts +103 -103
- package/src/module/meta/service/media-data.service.ts +507 -507
- package/src/module/meta/service/populate-meta.service.ts +193 -193
- package/src/module/meta/service/preference.service.ts +16 -16
- package/src/module/meta/service/resolver.service.ts +271 -271
- package/src/module/meta/service/section-master.service.ts +104 -104
- package/src/module/meta/service/update-form-json.service.ts +22 -22
- package/src/module/meta/service/user-app-mapping.service.ts +17 -17
- package/src/module/meta/service/view-master.service.ts +127 -127
- package/src/module/microservice-client/microservice-clients.module.ts +13 -13
- package/src/module/microservice-client/service/microservice-client-factory.ts +37 -37
- package/src/module/microservice-client/service/microservice-clients.ts +4 -4
- package/src/module/module/controller/menu.controller.ts +15 -15
- package/src/module/module/controller/module-access.controller.ts +134 -134
- package/src/module/module/entity/menu.entity.ts +43 -43
- package/src/module/module/entity/module-access.entity.ts +25 -25
- package/src/module/module/entity/module-action.entity.ts +17 -17
- package/src/module/module/entity/module.entity.ts +52 -52
- package/src/module/module/module.module.ts +42 -42
- package/src/module/module/repository/menu.repository.ts +184 -184
- package/src/module/module/repository/module-access.repository.ts +344 -344
- package/src/module/module/service/menu.service.ts +82 -82
- package/src/module/module/service/module-access.service.ts +194 -194
- package/src/module/notification/controller/notification.controller.ts +58 -58
- package/src/module/notification/controller/otp.controller.ts +117 -117
- package/src/module/notification/entity/notification.entity.ts +26 -26
- package/src/module/notification/entity/otp.entity.ts +28 -28
- package/src/module/notification/firebase-admin.config.ts +22 -22
- package/src/module/notification/notification.module.ts +69 -69
- package/src/module/notification/repository/otp.repository.ts +27 -27
- package/src/module/notification/service/email.service.ts +127 -127
- package/src/module/notification/service/notification.service.ts +160 -160
- package/src/module/notification/service/otp.service.ts +133 -133
- package/src/module/third-party-module/entity/third-party-api-registry.entity.ts +52 -52
- package/src/module/third-party-module/repository/third-party-api-registry.repository.ts +20 -20
- package/src/module/third-party-module/service/api-registry.service.ts +13 -13
- package/src/module/third-party-module/third-party.module.ts +12 -12
- package/src/module/user/controller/login.controller.ts +198 -198
- package/src/module/user/controller/user.controller.ts +40 -40
- package/src/module/user/dto/create-user.dto.ts +62 -62
- package/src/module/user/dto/update-user.dto.ts +4 -4
- package/src/module/user/entity/role.entity.ts +33 -33
- package/src/module/user/entity/user-role-mapping.entity.ts +38 -38
- package/src/module/user/entity/user-session.entity.ts +73 -73
- package/src/module/user/entity/user.entity.ts +59 -59
- package/src/module/user/repository/role.repository.ts +96 -96
- package/src/module/user/repository/user-role-mapping.repository.ts +126 -126
- package/src/module/user/repository/user.repository.ts +50 -50
- package/src/module/user/repository/userSession.repository.ts +33 -33
- package/src/module/user/service/login.service.ts +326 -326
- package/src/module/user/service/role.service.ts +189 -189
- package/src/module/user/service/user-role-mapping.service.ts +98 -98
- package/src/module/user/service/user-session.service.ts +168 -168
- package/src/module/user/service/user.service.ts +368 -368
- package/src/module/user/user.module.ts +65 -65
- package/src/module/workflow/controller/action-category.controller.ts +54 -54
- package/src/module/workflow/controller/action-resource-mapping.controller.ts +23 -23
- package/src/module/workflow/controller/action-template-mapping.controller.ts +35 -35
- package/src/module/workflow/controller/action.controller.ts +111 -111
- package/src/module/workflow/controller/activity-log.controller.ts +55 -55
- package/src/module/workflow/controller/comm-template.controller.ts +43 -43
- package/src/module/workflow/controller/entity-modification.controller.ts +35 -35
- package/src/module/workflow/controller/form-master.controller.ts +43 -43
- package/src/module/workflow/controller/stage-group.controller.ts +48 -48
- package/src/module/workflow/controller/stage.controller.ts +50 -50
- package/src/module/workflow/controller/task.controller.ts +77 -77
- package/src/module/workflow/controller/workflow-list-master.controller.ts +44 -44
- package/src/module/workflow/controller/workflow-meta.controller.ts +80 -80
- package/src/module/workflow/controller/workflow.controller.ts +67 -67
- package/src/module/workflow/entity/action-category.entity.ts +38 -38
- package/src/module/workflow/entity/action-data.entity.ts +55 -55
- package/src/module/workflow/entity/action-resources-mapping.entity.ts +29 -29
- package/src/module/workflow/entity/action-template-mapping.entity.ts +17 -17
- package/src/module/workflow/entity/action.entity.ts +53 -53
- package/src/module/workflow/entity/activity-log.entity.ts +43 -43
- package/src/module/workflow/entity/comm-template.entity.ts +43 -43
- package/src/module/workflow/entity/entity-modification.entity.ts +38 -38
- package/src/module/workflow/entity/form.entity.ts +25 -25
- package/src/module/workflow/entity/stage-action-mapping.entity.ts +17 -17
- package/src/module/workflow/entity/stage-group.entity.ts +23 -23
- package/src/module/workflow/entity/stage-movement-data.entity.ts +38 -38
- package/src/module/workflow/entity/stage.entity.ts +20 -20
- package/src/module/workflow/entity/task-data.entity.ts +88 -88
- package/src/module/workflow/entity/template-attach-mapper.entity.ts +30 -30
- package/src/module/workflow/entity/workflow-data.entity.ts +11 -11
- package/src/module/workflow/entity/workflow-level-mapping.entity.ts +18 -18
- package/src/module/workflow/entity/workflow.entity.ts +20 -20
- package/src/module/workflow/repository/action-category.repository.ts +79 -79
- package/src/module/workflow/repository/action-data.repository.ts +334 -334
- package/src/module/workflow/repository/action.repository.ts +324 -323
- package/src/module/workflow/repository/activity-log.repository.ts +148 -148
- package/src/module/workflow/repository/comm-template.repository.ts +149 -149
- package/src/module/workflow/repository/form-master.repository.ts +59 -59
- package/src/module/workflow/repository/stage-group.repository.ts +197 -197
- package/src/module/workflow/repository/stage-movement.repository.ts +246 -246
- package/src/module/workflow/repository/stage.repository.ts +172 -172
- package/src/module/workflow/repository/task.repository.ts +134 -134
- package/src/module/workflow/repository/workflow.repository.ts +42 -42
- package/src/module/workflow/service/action-category.service.ts +33 -33
- package/src/module/workflow/service/action-data.service.ts +62 -62
- package/src/module/workflow/service/action-resources-mapping.service.ts +10 -10
- package/src/module/workflow/service/action-template-mapping.service.ts +115 -115
- package/src/module/workflow/service/action.service.ts +279 -279
- package/src/module/workflow/service/activity-log.service.ts +107 -107
- package/src/module/workflow/service/comm-template.service.ts +180 -180
- package/src/module/workflow/service/entity-modification.service.ts +61 -61
- package/src/module/workflow/service/form-master.service.ts +35 -35
- package/src/module/workflow/service/populate-workflow.service.ts +303 -303
- package/src/module/workflow/service/stage-action-mapping.service.ts +5 -5
- package/src/module/workflow/service/stage-group.service.ts +342 -342
- package/src/module/workflow/service/stage.service.ts +199 -199
- package/src/module/workflow/service/task.service.ts +558 -558
- package/src/module/workflow/service/workflow-list-master.service.ts +60 -60
- package/src/module/workflow/service/workflow-meta.service.ts +654 -654
- package/src/module/workflow/service/workflow.service.ts +205 -205
- package/src/module/workflow/workflow.module.ts +178 -178
- package/src/module/workflow-automation/SCHEDULING_GUIDE.md +145 -145
- package/src/module/workflow-automation/controller/workflow-automation.controller.ts +43 -43
- package/src/module/workflow-automation/entity/workflow-automation-action.entity.ts +26 -26
- package/src/module/workflow-automation/entity/workflow-automation.entity.ts +40 -40
- package/src/module/workflow-automation/interface/action.decorator.ts +7 -7
- package/src/module/workflow-automation/interface/action.interface.ts +5 -5
- package/src/module/workflow-automation/service/action-registery.service.ts +35 -35
- package/src/module/workflow-automation/service/schedule-handler.service.ts +168 -168
- package/src/module/workflow-automation/service/workflow-automation-engine.service.ts +224 -224
- package/src/module/workflow-automation/service/workflow-automation.service.ts +515 -515
- package/src/module/workflow-automation/workflow-automation.module.ts +54 -54
- package/src/module/workflow-schedule/INSTALLATION.md +244 -244
- package/src/module/workflow-schedule/MULTI_PROJECT_GUIDE.md +196 -196
- package/src/module/workflow-schedule/README.md +422 -422
- package/src/module/workflow-schedule/constants/schedule.constants.ts +48 -48
- package/src/module/workflow-schedule/controller/workflow-schedule.controller.ts +255 -255
- package/src/module/workflow-schedule/docs/CLAUDE_CODE_GUIDE.md +510 -510
- package/src/module/workflow-schedule/docs/CLAUDE_CODE_PROMPT.md +362 -362
- package/src/module/workflow-schedule/docs/RUN_CLAUDE_CODE.sh +68 -68
- package/src/module/workflow-schedule/dto/create-schedule.dto.ts +147 -147
- package/src/module/workflow-schedule/dto/get-execution-logs.dto.ts +119 -119
- package/src/module/workflow-schedule/dto/update-schedule.dto.ts +96 -96
- package/src/module/workflow-schedule/entities/scheduled-workflow.entity.ts +148 -148
- package/src/module/workflow-schedule/entities/workflow-execution-log.entity.ts +154 -154
- package/src/module/workflow-schedule/interfaces/schedule-job-data.interface.ts +53 -53
- package/src/module/workflow-schedule/interfaces/workflow-schedule-options.interface.ts +12 -12
- package/src/module/workflow-schedule/processors/schedule.processor.ts +584 -584
- package/src/module/workflow-schedule/service/workflow-schedule.service.ts +600 -600
- package/src/module/workflow-schedule/workflow-schedule.module.ts +67 -67
- package/src/resources/dev.properties.yaml +31 -31
- package/src/resources/local.properties.yaml +27 -27
- package/src/resources/properties.module.ts +12 -12
- package/src/resources/properties.yaml.ts +11 -11
- package/src/resources/uat.properties.yaml +31 -31
- package/src/table.config.ts +130 -130
- package/src/utils/dto/excel-data.dto.ts +14 -14
- package/src/utils/dto/excelsheet-data.dto.ts +5 -5
- package/src/utils/service/base64util.service.ts +18 -18
- package/src/utils/service/clockIDGenUtil.service.ts +21 -21
- package/src/utils/service/codeGenerator.service.ts +22 -22
- package/src/utils/service/dateUtil.service.ts +17 -17
- package/src/utils/service/encryptUtil.service.ts +97 -97
- package/src/utils/service/excel-helper.service.ts +72 -72
- package/src/utils/service/excelUtil.service.ts +15 -15
- package/src/utils/service/file-util.service.ts +11 -11
- package/src/utils/service/json-util.service.ts +23 -23
- package/src/utils/service/loggingUtil.service.ts +88 -88
- package/src/utils/service/reflection-helper.service.ts +62 -62
- package/src/utils/service/wbsCodeGen.service.ts +8 -8
- package/src/utils/utils.module.ts +27 -27
- package/tsconfig.build.json +4 -4
- package/tsconfig.json +24 -24
|
@@ -1,654 +1,654 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BadGatewayException,
|
|
3
|
-
Inject,
|
|
4
|
-
Injectable,
|
|
5
|
-
Logger,
|
|
6
|
-
} from '@nestjs/common';
|
|
7
|
-
import { ConfigService } from '@nestjs/config';
|
|
8
|
-
import { InjectRepository } from '@nestjs/typeorm';
|
|
9
|
-
import axios from 'axios';
|
|
10
|
-
import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service';
|
|
11
|
-
import { UserData } from 'src/module/user/entity/user.entity';
|
|
12
|
-
import { DataSource, Repository } from 'typeorm';
|
|
13
|
-
import { StageMovementData } from '../entity/stage-movement-data.entity';
|
|
14
|
-
import { ACTIVITY_CATEGORIES } from '../repository/activity-log.repository';
|
|
15
|
-
import { StageMovementRepository } from '../repository/stage-movement.repository';
|
|
16
|
-
import { TaskRepository } from '../repository/task.repository';
|
|
17
|
-
import { ActionDataService } from './action-data.service';
|
|
18
|
-
import { ActivityLogService } from './activity-log.service';
|
|
19
|
-
import { EntityModificationService } from './entity-modification.service';
|
|
20
|
-
import { TaskService } from './task.service';
|
|
21
|
-
|
|
22
|
-
@Injectable()
|
|
23
|
-
export class WorkflowMetaService extends EntityServiceImpl {
|
|
24
|
-
constructor(
|
|
25
|
-
@InjectRepository(StageMovementData)
|
|
26
|
-
private readonly stageMovementRepo: Repository<StageMovementData>,
|
|
27
|
-
private readonly stageMovementRepository: StageMovementRepository,
|
|
28
|
-
private readonly dataSource: DataSource,
|
|
29
|
-
private readonly taskRepository: TaskRepository,
|
|
30
|
-
private readonly actionDataService: ActionDataService,
|
|
31
|
-
@Inject('TaskService')
|
|
32
|
-
private readonly taskService: TaskService,
|
|
33
|
-
@Inject('ActivityLogService')
|
|
34
|
-
private readonly activityLogService: ActivityLogService,
|
|
35
|
-
@Inject('EntityModificationService')
|
|
36
|
-
private readonly modificationService: EntityModificationService,
|
|
37
|
-
private readonly configService: ConfigService,
|
|
38
|
-
) {
|
|
39
|
-
super();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// get first stage from stage table
|
|
43
|
-
async getFirstStage(
|
|
44
|
-
loggedInUser: UserData,
|
|
45
|
-
mapped_entity_type: string,
|
|
46
|
-
mapped_entity_id: number,
|
|
47
|
-
): Promise<any | null> {
|
|
48
|
-
// step1 find in workflow_level_mapping if not found with current level_id and level_type search in for ORG
|
|
49
|
-
// step2 find in stage_group => workflow_id
|
|
50
|
-
// step3 find in stage => stage_group_id
|
|
51
|
-
// step4 get first stage from stage table
|
|
52
|
-
return this.stageMovementRepository.getFirstStage({
|
|
53
|
-
loggedInUser,
|
|
54
|
-
mapped_entity_type,
|
|
55
|
-
mapped_entity_id,
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Get the current active stage for a given mapped entity
|
|
61
|
-
*/
|
|
62
|
-
async getCurrentStage(
|
|
63
|
-
mapped_entity_type: string,
|
|
64
|
-
mapped_entity_id: number,
|
|
65
|
-
loggedInUser: UserData,
|
|
66
|
-
): Promise<any | null> {
|
|
67
|
-
// Try to find the current stage movement entry
|
|
68
|
-
const currentStage = await this.stageMovementRepo.findOne({
|
|
69
|
-
where: {
|
|
70
|
-
mapped_entity_type,
|
|
71
|
-
mapped_entity_id,
|
|
72
|
-
is_current: 'Y',
|
|
73
|
-
},
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
if (!currentStage) {
|
|
77
|
-
// Fetch the latest stage movement for the given entity
|
|
78
|
-
const latestMovement = await this.stageMovementRepo.findOne({
|
|
79
|
-
where: {
|
|
80
|
-
mapped_entity_type,
|
|
81
|
-
mapped_entity_id,
|
|
82
|
-
},
|
|
83
|
-
order: { id: 'DESC' }, // Get the most recent movement
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
if (!latestMovement) return null;
|
|
87
|
-
|
|
88
|
-
const getGroupName = await this.dataSource.query(
|
|
89
|
-
`SELECT name FROM frm_wf_stage_group WHERE id = ${latestMovement?.stage_group_id}`,
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
return {
|
|
93
|
-
...latestMovement,
|
|
94
|
-
stage_group_name: getGroupName[0]?.name,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// get group name for the current stage
|
|
99
|
-
const getGroupName = await this.dataSource.query(
|
|
100
|
-
`SELECT name FROM frm_wf_stage_group WHERE id = ${currentStage?.stage_group_id}`,
|
|
101
|
-
);
|
|
102
|
-
|
|
103
|
-
// Return the found or newly created current stage movement
|
|
104
|
-
return {
|
|
105
|
-
...currentStage,
|
|
106
|
-
stage_group_name: getGroupName[0]?.name,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Get next stage ID based on current stage
|
|
112
|
-
* (Stub logic – replace with real stage transition resolution)
|
|
113
|
-
*/
|
|
114
|
-
async getNextStage(currentStage: any): Promise<any | null> {
|
|
115
|
-
// get all stage for that stage group
|
|
116
|
-
const nextStage =
|
|
117
|
-
await this.stageMovementRepository.getNextStageOrFirstOfNextGroup(
|
|
118
|
-
currentStage?.stage_group_id,
|
|
119
|
-
currentStage?.stage_id,
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
if (!nextStage) {
|
|
123
|
-
return { hasNextStage: false, nextStage: null };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const groupName = await this.dataSource.query(
|
|
127
|
-
`SELECT name FROM frm_wf_stage_group WHERE id = ${nextStage?.stage_group_id}`,
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
return {
|
|
131
|
-
hasNextStage: !!nextStage,
|
|
132
|
-
nextStage: { ...nextStage, stage_group_name: groupName[0]?.name },
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Move mapped entity to the next stage in the workflow
|
|
138
|
-
*/
|
|
139
|
-
async moveToNextStage(
|
|
140
|
-
mapped_entity_type: string,
|
|
141
|
-
mapped_entity_id: number,
|
|
142
|
-
loggedInUser: UserData,
|
|
143
|
-
reason_code?: string,
|
|
144
|
-
remark?: string,
|
|
145
|
-
): Promise<string> {
|
|
146
|
-
const now = new Date();
|
|
147
|
-
let currentStage = await this.getCurrentStage(
|
|
148
|
-
mapped_entity_type,
|
|
149
|
-
mapped_entity_id,
|
|
150
|
-
loggedInUser,
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
if (currentStage) {
|
|
154
|
-
await this.taskService.createSystemNote(
|
|
155
|
-
{
|
|
156
|
-
reason_code: reason_code || '',
|
|
157
|
-
remark: remark || '',
|
|
158
|
-
mapped_entity_id,
|
|
159
|
-
stage_id: currentStage.stage_id,
|
|
160
|
-
action_id: currentStage.action_id,
|
|
161
|
-
stage_group_id: currentStage.stage_group_id,
|
|
162
|
-
},
|
|
163
|
-
loggedInUser,
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Case 1: First stage – initialize
|
|
168
|
-
if (!currentStage) {
|
|
169
|
-
const getAllResult = await this.getFirstStage(
|
|
170
|
-
loggedInUser,
|
|
171
|
-
mapped_entity_type,
|
|
172
|
-
mapped_entity_id,
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
const { mappingUsed, stageGroup, firstStage } = getAllResult;
|
|
176
|
-
|
|
177
|
-
currentStage = await this.stageMovementRepo.save({
|
|
178
|
-
entity_type: 'WFSA',
|
|
179
|
-
mapped_entity_type,
|
|
180
|
-
mapped_entity_id,
|
|
181
|
-
name: firstStage.name,
|
|
182
|
-
status: firstStage.status,
|
|
183
|
-
current_user_id: loggedInUser.id,
|
|
184
|
-
stage_action_mapping_id: firstStage.id,
|
|
185
|
-
organization_id: loggedInUser.organization_id,
|
|
186
|
-
stage_group_id: stageGroup.id,
|
|
187
|
-
stage_id: firstStage.id,
|
|
188
|
-
start_date: new Date(),
|
|
189
|
-
is_current: 'Y',
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
await this.populateActionService(
|
|
193
|
-
currentStage.stage_id,
|
|
194
|
-
loggedInUser,
|
|
195
|
-
mapped_entity_id,
|
|
196
|
-
mapped_entity_type,
|
|
197
|
-
);
|
|
198
|
-
return `Initialized workflow with first stage (Stage ID: ${firstStage.id}).`;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Case 2: Has next stage – move forward
|
|
202
|
-
const stageData = await this.getNextStage(currentStage);
|
|
203
|
-
|
|
204
|
-
if (stageData.hasNextStage) {
|
|
205
|
-
const { nextStage } = stageData;
|
|
206
|
-
|
|
207
|
-
// ✅ NEW: Log stage group completion if group changes
|
|
208
|
-
if (nextStage.stage_group_id !== currentStage.stage_group_id) {
|
|
209
|
-
try {
|
|
210
|
-
// stage action category SGCP
|
|
211
|
-
// if current stage actions contain any action with action_category as SGCP then set the status of that action to completed
|
|
212
|
-
const stageActions =
|
|
213
|
-
await this.stageMovementRepository.getAllActionByStageId(
|
|
214
|
-
currentStage.stage_id,
|
|
215
|
-
);
|
|
216
|
-
|
|
217
|
-
for (const action of stageActions) {
|
|
218
|
-
const actionCategory = await this.dataSource.query(
|
|
219
|
-
`SELECT code FROM frm_wf_action_category WHERE id = ${Number(
|
|
220
|
-
action.action_category,
|
|
221
|
-
)}`,
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
if (actionCategory[0]?.code == 'SGCP') {
|
|
225
|
-
await this.taskRepository.updateTaskStatus(
|
|
226
|
-
loggedInUser,
|
|
227
|
-
mapped_entity_type,
|
|
228
|
-
mapped_entity_id,
|
|
229
|
-
currentStage.stage_id,
|
|
230
|
-
action.id,
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
await this.activityLogService.logActivity(
|
|
236
|
-
{
|
|
237
|
-
mapped_entity_id,
|
|
238
|
-
mapped_entity_type,
|
|
239
|
-
title: 'Stage Group Completed',
|
|
240
|
-
description: `Stage Group changed from ${currentStage.stage_group_name} to ${nextStage.stage_group_name}.`,
|
|
241
|
-
action: 'completed',
|
|
242
|
-
category: ACTIVITY_CATEGORIES.STAGEGROUP,
|
|
243
|
-
appcode: loggedInUser.appcode,
|
|
244
|
-
},
|
|
245
|
-
loggedInUser,
|
|
246
|
-
);
|
|
247
|
-
} catch (error) {
|
|
248
|
-
console.error(
|
|
249
|
-
'Failed to log stage group completion:',
|
|
250
|
-
error?.message || error,
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Close current stage
|
|
256
|
-
currentStage.end_date = now;
|
|
257
|
-
currentStage.is_current = 'N';
|
|
258
|
-
await this.stageMovementRepo.save(currentStage);
|
|
259
|
-
|
|
260
|
-
// Insert next stage
|
|
261
|
-
await this.stageMovementRepo.save({
|
|
262
|
-
entity_type: 'WFSA',
|
|
263
|
-
mapped_entity_type,
|
|
264
|
-
mapped_entity_id,
|
|
265
|
-
name: nextStage.name,
|
|
266
|
-
status: nextStage.status,
|
|
267
|
-
current_user_id: loggedInUser.id,
|
|
268
|
-
stage_action_mapping_id: nextStage.id,
|
|
269
|
-
organization_id: loggedInUser.organization_id,
|
|
270
|
-
start_date: now,
|
|
271
|
-
stage_group_id: nextStage?.stage_group_id,
|
|
272
|
-
stage_id: nextStage.id,
|
|
273
|
-
is_current: 'Y',
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
await this.populateActionService(
|
|
277
|
-
stageData.nextStage.id,
|
|
278
|
-
loggedInUser,
|
|
279
|
-
mapped_entity_id,
|
|
280
|
-
mapped_entity_type,
|
|
281
|
-
);
|
|
282
|
-
|
|
283
|
-
// Stage completion log
|
|
284
|
-
try {
|
|
285
|
-
await this.activityLogService.logActivity(
|
|
286
|
-
{
|
|
287
|
-
mapped_entity_id: mapped_entity_id,
|
|
288
|
-
mapped_entity_type: mapped_entity_type,
|
|
289
|
-
title: 'Stage Completed',
|
|
290
|
-
description: `Stage changed from ${currentStage.name} to ${nextStage.name}.`,
|
|
291
|
-
action: 'status',
|
|
292
|
-
category: ACTIVITY_CATEGORIES.STAGE,
|
|
293
|
-
appcode: loggedInUser.appcode,
|
|
294
|
-
},
|
|
295
|
-
loggedInUser,
|
|
296
|
-
);
|
|
297
|
-
} catch (error) {
|
|
298
|
-
console.error(
|
|
299
|
-
'Failed to log activity for stage completion:',
|
|
300
|
-
error?.message || error,
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
return `Moved to next stage (Stage ID: ${nextStage.id}).`;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// Case 3: No next stage – mark workflow as done
|
|
307
|
-
currentStage.end_date = now;
|
|
308
|
-
currentStage.is_current = 'N';
|
|
309
|
-
await this.stageMovementRepo.save(currentStage);
|
|
310
|
-
|
|
311
|
-
return 'Workflow completed. No next stage available.';
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
async populateActionService(
|
|
315
|
-
stage_id: number,
|
|
316
|
-
loggedInUser: UserData,
|
|
317
|
-
mapped_entity_id: number,
|
|
318
|
-
mapped_entity_type: string,
|
|
319
|
-
): Promise<any> {
|
|
320
|
-
const actions =
|
|
321
|
-
await this.stageMovementRepository.getAllActionByStageId(stage_id);
|
|
322
|
-
// Populate the action service with the retrieved actions
|
|
323
|
-
|
|
324
|
-
if (!actions || actions.length === 0) {
|
|
325
|
-
return 'No actions found for this stage.';
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
// check whether first action's action_category is owner_assignment and assignment_type is AUTO_ASSIGN
|
|
329
|
-
const firstAction = actions[0];
|
|
330
|
-
const actionCategory = await this.dataSource.query(
|
|
331
|
-
`SELECT code FROM frm_wf_action_category WHERE id = ${Number(firstAction.action_category)}`,
|
|
332
|
-
);
|
|
333
|
-
|
|
334
|
-
const assignmentType = await this.dataSource.query(
|
|
335
|
-
`SELECT value FROM frm_list_master_items WHERE id = ${Number(firstAction.assignment_type)}`,
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
// save in action data
|
|
339
|
-
await this.actionDataService.saveActionData(
|
|
340
|
-
actions,
|
|
341
|
-
loggedInUser,
|
|
342
|
-
mapped_entity_id,
|
|
343
|
-
mapped_entity_type,
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
// save in task data
|
|
347
|
-
await this.taskService.saveActionData(
|
|
348
|
-
actions,
|
|
349
|
-
loggedInUser,
|
|
350
|
-
mapped_entity_id,
|
|
351
|
-
mapped_entity_type,
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
if (
|
|
355
|
-
actionCategory[0]?.code == 'OWAS' &&
|
|
356
|
-
assignmentType[0]?.value == 'round_robin'
|
|
357
|
-
) {
|
|
358
|
-
console.log('Auto-assigning owner based on round-robin assignment type');
|
|
359
|
-
await this.assignLead(
|
|
360
|
-
loggedInUser,
|
|
361
|
-
mapped_entity_id,
|
|
362
|
-
mapped_entity_type,
|
|
363
|
-
stage_id,
|
|
364
|
-
actions,
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
async updateLeadOwner(entityData, loggedInUser) {
|
|
370
|
-
const { lead_id, lead_owner, stage_id, entity_type } = entityData;
|
|
371
|
-
const updatedData = {
|
|
372
|
-
id: lead_id,
|
|
373
|
-
lead_owner: lead_owner,
|
|
374
|
-
entity_type: entity_type,
|
|
375
|
-
};
|
|
376
|
-
entityData = updatedData;
|
|
377
|
-
|
|
378
|
-
this.modificationService.logModification(
|
|
379
|
-
{
|
|
380
|
-
entity_type: 'ENMD',
|
|
381
|
-
mapped_entity_type: entity_type,
|
|
382
|
-
mapped_entity_id: lead_id,
|
|
383
|
-
attribute_key: 'lead_owner',
|
|
384
|
-
new_value: lead_owner,
|
|
385
|
-
old_value: entityData.lead_owner,
|
|
386
|
-
remarks: entityData.remarks,
|
|
387
|
-
reason_code: entityData.reason_code,
|
|
388
|
-
stage_id: stage_id,
|
|
389
|
-
action_id: entityData.action_id,
|
|
390
|
-
},
|
|
391
|
-
loggedInUser,
|
|
392
|
-
);
|
|
393
|
-
|
|
394
|
-
const leadData: any = await this.getEntityData(
|
|
395
|
-
'LEAD',
|
|
396
|
-
lead_id,
|
|
397
|
-
loggedInUser,
|
|
398
|
-
);
|
|
399
|
-
|
|
400
|
-
const unassignedListMasterItemData = await this.dataSource.query(
|
|
401
|
-
`SELECT name, id
|
|
402
|
-
FROM frm_list_master_items
|
|
403
|
-
WHERE listtype = "LEST" AND organization_id = $1
|
|
404
|
-
AND value IN ($2, $3)`,
|
|
405
|
-
[loggedInUser.organization_id, 'unassigned', 'active'],
|
|
406
|
-
);
|
|
407
|
-
|
|
408
|
-
// Find the IDs explicitly
|
|
409
|
-
const unassignedId = unassignedListMasterItemData.find(
|
|
410
|
-
(item) => item.name.toLowerCase() === 'unassigned',
|
|
411
|
-
)?.id;
|
|
412
|
-
|
|
413
|
-
const activeId = unassignedListMasterItemData.find(
|
|
414
|
-
(item) => item.name.toLowerCase() === 'active',
|
|
415
|
-
)?.id;
|
|
416
|
-
|
|
417
|
-
// Use unassignedId for comparison
|
|
418
|
-
if (leadData?.lead_status === unassignedId) {
|
|
419
|
-
leadData.lead_status = activeId;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
const result = await super.updateEntity(
|
|
423
|
-
{
|
|
424
|
-
...entityData,
|
|
425
|
-
status: leadData?.lead_status,
|
|
426
|
-
lead_status: leadData?.lead_status,
|
|
427
|
-
},
|
|
428
|
-
loggedInUser,
|
|
429
|
-
);
|
|
430
|
-
|
|
431
|
-
await this.dataSource.query(
|
|
432
|
-
`UPDATE frm_wf_action_data SET user_id =$1
|
|
433
|
-
WHERE mapped_entity_id=$2
|
|
434
|
-
AND mapped_entity_type = $3
|
|
435
|
-
AND stage_id = $4
|
|
436
|
-
AND (is_current = 'Y' OR is_current IS NULL)
|
|
437
|
-
`,
|
|
438
|
-
[lead_owner, lead_id, entity_type, stage_id],
|
|
439
|
-
);
|
|
440
|
-
|
|
441
|
-
await this.dataSource.query(
|
|
442
|
-
`UPDATE crm_lead_meeting SET user_id=$1
|
|
443
|
-
WHERE stage_id = $2
|
|
444
|
-
AND mapped_entity_id = $3
|
|
445
|
-
AND mapped_entity_type = "LEAD"
|
|
446
|
-
AND (status='scheduled' OR status='rescheduled')
|
|
447
|
-
`,
|
|
448
|
-
[lead_owner, stage_id, lead_id],
|
|
449
|
-
);
|
|
450
|
-
|
|
451
|
-
const taskRows = await this.dataSource.query(
|
|
452
|
-
`SELECT id,status FROM frm_wf_task_data
|
|
453
|
-
WHERE mapped_entity_id = $1
|
|
454
|
-
AND mapped_entity_type = $2
|
|
455
|
-
AND stage_id = $3`,
|
|
456
|
-
[lead_id, entity_type, stage_id],
|
|
457
|
-
);
|
|
458
|
-
|
|
459
|
-
for (const task of taskRows) {
|
|
460
|
-
const statusRows = await this.dataSource.query(
|
|
461
|
-
`SELECT value FROM frm_list_master_items
|
|
462
|
-
WHERE id = $1 AND organization_id = $2`,
|
|
463
|
-
[task.status, loggedInUser.organization_id],
|
|
464
|
-
);
|
|
465
|
-
|
|
466
|
-
const statusName = statusRows[0]?.value?.toLowerCase() || '';
|
|
467
|
-
if (
|
|
468
|
-
['todo', 'in_progress'].includes(statusName) ||
|
|
469
|
-
statusName === 'todo'
|
|
470
|
-
) {
|
|
471
|
-
await this.dataSource.query(
|
|
472
|
-
`UPDATE frm_wf_task_data SET user_id = $1, task_owner = $2 WHERE id = $3`,
|
|
473
|
-
[lead_owner, lead_owner, task.id],
|
|
474
|
-
);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
let leadOwnerName;
|
|
479
|
-
|
|
480
|
-
try {
|
|
481
|
-
const baseUrl = this.configService.get<string>('REDIRECT_BE_URL');
|
|
482
|
-
|
|
483
|
-
// Prepare query params
|
|
484
|
-
const queryParams = new URLSearchParams({
|
|
485
|
-
loggedInUser: JSON.stringify(loggedInUser),
|
|
486
|
-
}).toString();
|
|
487
|
-
|
|
488
|
-
const url = `${baseUrl}/entity/public/getById/${entityData?.lead_owner}?entity_type=USR&${queryParams}`;
|
|
489
|
-
|
|
490
|
-
const response = await axios.get(url);
|
|
491
|
-
|
|
492
|
-
leadOwnerName = response.data;
|
|
493
|
-
} catch (error) {
|
|
494
|
-
console.error('Internal Entity API call failed:', error.message);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
try {
|
|
498
|
-
const logData = {
|
|
499
|
-
mapped_entity_id: lead_id,
|
|
500
|
-
mapped_entity_type: 'LEAD',
|
|
501
|
-
title: 'Owner Assigned',
|
|
502
|
-
description: leadData?.lead_owner
|
|
503
|
-
? `${leadOwnerName?.name} reassigned as Lead owner.`
|
|
504
|
-
: `${leadOwnerName?.name} assigned as Lead owner.`,
|
|
505
|
-
action: 'assign',
|
|
506
|
-
category: ACTIVITY_CATEGORIES.ASSIGN,
|
|
507
|
-
appcode: loggedInUser.appcode,
|
|
508
|
-
};
|
|
509
|
-
|
|
510
|
-
await this.activityLogService.logActivity(logData, loggedInUser);
|
|
511
|
-
} catch (error) {
|
|
512
|
-
console.error(
|
|
513
|
-
'Failed to log activity for meeting:',
|
|
514
|
-
error?.message || error,
|
|
515
|
-
);
|
|
516
|
-
// Logging should not block main flow
|
|
517
|
-
}
|
|
518
|
-
return result;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
async assignLead(
|
|
522
|
-
loggedInUser: UserData,
|
|
523
|
-
mapped_entity_id: number,
|
|
524
|
-
mapped_entity_type: string,
|
|
525
|
-
stage_id: number,
|
|
526
|
-
actions: any[],
|
|
527
|
-
): Promise<number> {
|
|
528
|
-
const { organization_id, level_id, level_type } = loggedInUser;
|
|
529
|
-
|
|
530
|
-
// 1) Get eligible owners (distinct + stable ordering) // axios call
|
|
531
|
-
// const owners: Array<{ id: number }> = await this.dataSource.query(
|
|
532
|
-
// `
|
|
533
|
-
// SELECT DISTINCT u.id
|
|
534
|
-
// FROM sso_user u
|
|
535
|
-
// JOIN sso_user_role_mapping urm ON u.id = urm.user_id
|
|
536
|
-
// JOIN sso_role r ON urm.role_id = r.id
|
|
537
|
-
// JOIN sso_module_access ma ON ma.role_code = r.code
|
|
538
|
-
// WHERE urm.organization_id = ?
|
|
539
|
-
// AND urm.level_id = ?
|
|
540
|
-
// AND urm.level_type = ?
|
|
541
|
-
// AND urm.appcode = 'CRM'
|
|
542
|
-
// AND ma.appcode = 'CRM'
|
|
543
|
-
// AND ma.module_code = 'lead_crm_sch'
|
|
544
|
-
// AND ma.access_flag = 1
|
|
545
|
-
// AND ma.action_type = 'LEAD_OWNER'
|
|
546
|
-
// ORDER BY u.id ASC
|
|
547
|
-
// `,
|
|
548
|
-
// [organization_id, Number(level_id), level_type],
|
|
549
|
-
// );
|
|
550
|
-
|
|
551
|
-
let owners = [] as any;
|
|
552
|
-
|
|
553
|
-
try {
|
|
554
|
-
const baseUrl = this.configService.get<string>('REDIRECT_BE_URL');
|
|
555
|
-
|
|
556
|
-
// Prepare query params
|
|
557
|
-
const queryParams = new URLSearchParams({
|
|
558
|
-
loggedInUser: JSON.stringify(loggedInUser),
|
|
559
|
-
}).toString();
|
|
560
|
-
|
|
561
|
-
const { level_id } = loggedInUser;
|
|
562
|
-
|
|
563
|
-
const url = `${baseUrl}/users/public/lead-owners/?entity_type=USR&${queryParams}`;
|
|
564
|
-
|
|
565
|
-
const response = await axios.get(url);
|
|
566
|
-
|
|
567
|
-
owners = response.data;
|
|
568
|
-
|
|
569
|
-
// The API response is likely a JSON object, not an array
|
|
570
|
-
} catch (error) {
|
|
571
|
-
console.error('Internal Entity API call failed:', error.message);
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
if (!owners?.length)
|
|
575
|
-
console.log('No eligible owners found for lead assignment.');
|
|
576
|
-
const userIds = owners?.map((o) => Number(o.id)); // normalize to numbers
|
|
577
|
-
|
|
578
|
-
// 2) Find the last assigned *eligible* owner (use IN (...))
|
|
579
|
-
const placeholders = userIds.map(() => '?').join(',');
|
|
580
|
-
const lastRow: Array<{ lead_owner: number }> = await this.dataSource.query(
|
|
581
|
-
`
|
|
582
|
-
SELECT lead_owner
|
|
583
|
-
FROM crm_lead
|
|
584
|
-
WHERE organization_id = $1
|
|
585
|
-
AND level_id = $2
|
|
586
|
-
AND level_type = $3
|
|
587
|
-
AND lead_owner IN (${placeholders})
|
|
588
|
-
ORDER BY created_date DESC
|
|
589
|
-
LIMIT 1
|
|
590
|
-
`,
|
|
591
|
-
[organization_id, Number(level_id), level_type, ...userIds],
|
|
592
|
-
);
|
|
593
|
-
|
|
594
|
-
const lastAssigned = lastRow.length ? Number(lastRow[0].lead_owner) : null;
|
|
595
|
-
|
|
596
|
-
// 3) Compute next user in round-robin
|
|
597
|
-
const lastIdx = lastAssigned != null ? userIds.indexOf(lastAssigned) : -1;
|
|
598
|
-
const nextIdx = (lastIdx + 1) % userIds.length;
|
|
599
|
-
const nextUser = userIds[nextIdx];
|
|
600
|
-
|
|
601
|
-
//4) Update lead owner in the lead table no need because we are updating lead owner in updateLeadOwner method
|
|
602
|
-
// await this.dataSource.query(
|
|
603
|
-
// `
|
|
604
|
-
// UPDATE crm_lead
|
|
605
|
-
// SET lead_owner = ?
|
|
606
|
-
// WHERE organization_id = ?
|
|
607
|
-
// AND level_id = ?
|
|
608
|
-
// AND level_type = ?
|
|
609
|
-
// AND lead_owner IS NULL
|
|
610
|
-
// ORDER BY created_date ASC
|
|
611
|
-
// LIMIT 1
|
|
612
|
-
// `,
|
|
613
|
-
// [nextUser, organization_id, Number(level_id), level_type],
|
|
614
|
-
// );
|
|
615
|
-
|
|
616
|
-
console.log(`Assigning lead to user ID: ${nextUser}`);
|
|
617
|
-
|
|
618
|
-
const firstAction = actions[0];
|
|
619
|
-
|
|
620
|
-
// update lead owner status
|
|
621
|
-
await this.updateLeadOwner(
|
|
622
|
-
{
|
|
623
|
-
lead_id: mapped_entity_id,
|
|
624
|
-
lead_owner: nextUser,
|
|
625
|
-
stage_id: stage_id,
|
|
626
|
-
entity_type: mapped_entity_type,
|
|
627
|
-
action_id: firstAction.id,
|
|
628
|
-
},
|
|
629
|
-
loggedInUser,
|
|
630
|
-
);
|
|
631
|
-
|
|
632
|
-
// move task
|
|
633
|
-
await this.taskService.moveTask(loggedInUser, {
|
|
634
|
-
mapped_entity_type,
|
|
635
|
-
mapped_entity_id,
|
|
636
|
-
stage_id,
|
|
637
|
-
action_id: firstAction.id,
|
|
638
|
-
});
|
|
639
|
-
|
|
640
|
-
// if it has only one action then move next stage
|
|
641
|
-
if (actions.length == 1) {
|
|
642
|
-
console.log(
|
|
643
|
-
'Only one action present and it is owner assignment. Moving to next stage.',
|
|
644
|
-
);
|
|
645
|
-
await this.moveToNextStage(
|
|
646
|
-
mapped_entity_type,
|
|
647
|
-
mapped_entity_id,
|
|
648
|
-
loggedInUser,
|
|
649
|
-
);
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
return nextUser;
|
|
653
|
-
}
|
|
654
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
BadGatewayException,
|
|
3
|
+
Inject,
|
|
4
|
+
Injectable,
|
|
5
|
+
Logger,
|
|
6
|
+
} from '@nestjs/common';
|
|
7
|
+
import { ConfigService } from '@nestjs/config';
|
|
8
|
+
import { InjectRepository } from '@nestjs/typeorm';
|
|
9
|
+
import axios from 'axios';
|
|
10
|
+
import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service';
|
|
11
|
+
import { UserData } from 'src/module/user/entity/user.entity';
|
|
12
|
+
import { DataSource, Repository } from 'typeorm';
|
|
13
|
+
import { StageMovementData } from '../entity/stage-movement-data.entity';
|
|
14
|
+
import { ACTIVITY_CATEGORIES } from '../repository/activity-log.repository';
|
|
15
|
+
import { StageMovementRepository } from '../repository/stage-movement.repository';
|
|
16
|
+
import { TaskRepository } from '../repository/task.repository';
|
|
17
|
+
import { ActionDataService } from './action-data.service';
|
|
18
|
+
import { ActivityLogService } from './activity-log.service';
|
|
19
|
+
import { EntityModificationService } from './entity-modification.service';
|
|
20
|
+
import { TaskService } from './task.service';
|
|
21
|
+
|
|
22
|
+
@Injectable()
|
|
23
|
+
export class WorkflowMetaService extends EntityServiceImpl {
|
|
24
|
+
constructor(
|
|
25
|
+
@InjectRepository(StageMovementData)
|
|
26
|
+
private readonly stageMovementRepo: Repository<StageMovementData>,
|
|
27
|
+
private readonly stageMovementRepository: StageMovementRepository,
|
|
28
|
+
private readonly dataSource: DataSource,
|
|
29
|
+
private readonly taskRepository: TaskRepository,
|
|
30
|
+
private readonly actionDataService: ActionDataService,
|
|
31
|
+
@Inject('TaskService')
|
|
32
|
+
private readonly taskService: TaskService,
|
|
33
|
+
@Inject('ActivityLogService')
|
|
34
|
+
private readonly activityLogService: ActivityLogService,
|
|
35
|
+
@Inject('EntityModificationService')
|
|
36
|
+
private readonly modificationService: EntityModificationService,
|
|
37
|
+
private readonly configService: ConfigService,
|
|
38
|
+
) {
|
|
39
|
+
super();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// get first stage from stage table
|
|
43
|
+
async getFirstStage(
|
|
44
|
+
loggedInUser: UserData,
|
|
45
|
+
mapped_entity_type: string,
|
|
46
|
+
mapped_entity_id: number,
|
|
47
|
+
): Promise<any | null> {
|
|
48
|
+
// step1 find in workflow_level_mapping if not found with current level_id and level_type search in for ORG
|
|
49
|
+
// step2 find in stage_group => workflow_id
|
|
50
|
+
// step3 find in stage => stage_group_id
|
|
51
|
+
// step4 get first stage from stage table
|
|
52
|
+
return this.stageMovementRepository.getFirstStage({
|
|
53
|
+
loggedInUser,
|
|
54
|
+
mapped_entity_type,
|
|
55
|
+
mapped_entity_id,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get the current active stage for a given mapped entity
|
|
61
|
+
*/
|
|
62
|
+
async getCurrentStage(
|
|
63
|
+
mapped_entity_type: string,
|
|
64
|
+
mapped_entity_id: number,
|
|
65
|
+
loggedInUser: UserData,
|
|
66
|
+
): Promise<any | null> {
|
|
67
|
+
// Try to find the current stage movement entry
|
|
68
|
+
const currentStage = await this.stageMovementRepo.findOne({
|
|
69
|
+
where: {
|
|
70
|
+
mapped_entity_type,
|
|
71
|
+
mapped_entity_id,
|
|
72
|
+
is_current: 'Y',
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
if (!currentStage) {
|
|
77
|
+
// Fetch the latest stage movement for the given entity
|
|
78
|
+
const latestMovement = await this.stageMovementRepo.findOne({
|
|
79
|
+
where: {
|
|
80
|
+
mapped_entity_type,
|
|
81
|
+
mapped_entity_id,
|
|
82
|
+
},
|
|
83
|
+
order: { id: 'DESC' }, // Get the most recent movement
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (!latestMovement) return null;
|
|
87
|
+
|
|
88
|
+
const getGroupName = await this.dataSource.query(
|
|
89
|
+
`SELECT name FROM frm_wf_stage_group WHERE id = ${latestMovement?.stage_group_id}`,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
...latestMovement,
|
|
94
|
+
stage_group_name: getGroupName[0]?.name,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// get group name for the current stage
|
|
99
|
+
const getGroupName = await this.dataSource.query(
|
|
100
|
+
`SELECT name FROM frm_wf_stage_group WHERE id = ${currentStage?.stage_group_id}`,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// Return the found or newly created current stage movement
|
|
104
|
+
return {
|
|
105
|
+
...currentStage,
|
|
106
|
+
stage_group_name: getGroupName[0]?.name,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Get next stage ID based on current stage
|
|
112
|
+
* (Stub logic – replace with real stage transition resolution)
|
|
113
|
+
*/
|
|
114
|
+
async getNextStage(currentStage: any): Promise<any | null> {
|
|
115
|
+
// get all stage for that stage group
|
|
116
|
+
const nextStage =
|
|
117
|
+
await this.stageMovementRepository.getNextStageOrFirstOfNextGroup(
|
|
118
|
+
currentStage?.stage_group_id,
|
|
119
|
+
currentStage?.stage_id,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
if (!nextStage) {
|
|
123
|
+
return { hasNextStage: false, nextStage: null };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const groupName = await this.dataSource.query(
|
|
127
|
+
`SELECT name FROM frm_wf_stage_group WHERE id = ${nextStage?.stage_group_id}`,
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
hasNextStage: !!nextStage,
|
|
132
|
+
nextStage: { ...nextStage, stage_group_name: groupName[0]?.name },
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Move mapped entity to the next stage in the workflow
|
|
138
|
+
*/
|
|
139
|
+
async moveToNextStage(
|
|
140
|
+
mapped_entity_type: string,
|
|
141
|
+
mapped_entity_id: number,
|
|
142
|
+
loggedInUser: UserData,
|
|
143
|
+
reason_code?: string,
|
|
144
|
+
remark?: string,
|
|
145
|
+
): Promise<string> {
|
|
146
|
+
const now = new Date();
|
|
147
|
+
let currentStage = await this.getCurrentStage(
|
|
148
|
+
mapped_entity_type,
|
|
149
|
+
mapped_entity_id,
|
|
150
|
+
loggedInUser,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
if (currentStage) {
|
|
154
|
+
await this.taskService.createSystemNote(
|
|
155
|
+
{
|
|
156
|
+
reason_code: reason_code || '',
|
|
157
|
+
remark: remark || '',
|
|
158
|
+
mapped_entity_id,
|
|
159
|
+
stage_id: currentStage.stage_id,
|
|
160
|
+
action_id: currentStage.action_id,
|
|
161
|
+
stage_group_id: currentStage.stage_group_id,
|
|
162
|
+
},
|
|
163
|
+
loggedInUser,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Case 1: First stage – initialize
|
|
168
|
+
if (!currentStage) {
|
|
169
|
+
const getAllResult = await this.getFirstStage(
|
|
170
|
+
loggedInUser,
|
|
171
|
+
mapped_entity_type,
|
|
172
|
+
mapped_entity_id,
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
const { mappingUsed, stageGroup, firstStage } = getAllResult;
|
|
176
|
+
|
|
177
|
+
currentStage = await this.stageMovementRepo.save({
|
|
178
|
+
entity_type: 'WFSA',
|
|
179
|
+
mapped_entity_type,
|
|
180
|
+
mapped_entity_id,
|
|
181
|
+
name: firstStage.name,
|
|
182
|
+
status: firstStage.status,
|
|
183
|
+
current_user_id: loggedInUser.id,
|
|
184
|
+
stage_action_mapping_id: firstStage.id,
|
|
185
|
+
organization_id: loggedInUser.organization_id,
|
|
186
|
+
stage_group_id: stageGroup.id,
|
|
187
|
+
stage_id: firstStage.id,
|
|
188
|
+
start_date: new Date(),
|
|
189
|
+
is_current: 'Y',
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
await this.populateActionService(
|
|
193
|
+
currentStage.stage_id,
|
|
194
|
+
loggedInUser,
|
|
195
|
+
mapped_entity_id,
|
|
196
|
+
mapped_entity_type,
|
|
197
|
+
);
|
|
198
|
+
return `Initialized workflow with first stage (Stage ID: ${firstStage.id}).`;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Case 2: Has next stage – move forward
|
|
202
|
+
const stageData = await this.getNextStage(currentStage);
|
|
203
|
+
|
|
204
|
+
if (stageData.hasNextStage) {
|
|
205
|
+
const { nextStage } = stageData;
|
|
206
|
+
|
|
207
|
+
// ✅ NEW: Log stage group completion if group changes
|
|
208
|
+
if (nextStage.stage_group_id !== currentStage.stage_group_id) {
|
|
209
|
+
try {
|
|
210
|
+
// stage action category SGCP
|
|
211
|
+
// if current stage actions contain any action with action_category as SGCP then set the status of that action to completed
|
|
212
|
+
const stageActions =
|
|
213
|
+
await this.stageMovementRepository.getAllActionByStageId(
|
|
214
|
+
currentStage.stage_id,
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
for (const action of stageActions) {
|
|
218
|
+
const actionCategory = await this.dataSource.query(
|
|
219
|
+
`SELECT code FROM frm_wf_action_category WHERE id = ${Number(
|
|
220
|
+
action.action_category,
|
|
221
|
+
)}`,
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
if (actionCategory[0]?.code == 'SGCP') {
|
|
225
|
+
await this.taskRepository.updateTaskStatus(
|
|
226
|
+
loggedInUser,
|
|
227
|
+
mapped_entity_type,
|
|
228
|
+
mapped_entity_id,
|
|
229
|
+
currentStage.stage_id,
|
|
230
|
+
action.id,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
await this.activityLogService.logActivity(
|
|
236
|
+
{
|
|
237
|
+
mapped_entity_id,
|
|
238
|
+
mapped_entity_type,
|
|
239
|
+
title: 'Stage Group Completed',
|
|
240
|
+
description: `Stage Group changed from ${currentStage.stage_group_name} to ${nextStage.stage_group_name}.`,
|
|
241
|
+
action: 'completed',
|
|
242
|
+
category: ACTIVITY_CATEGORIES.STAGEGROUP,
|
|
243
|
+
appcode: loggedInUser.appcode,
|
|
244
|
+
},
|
|
245
|
+
loggedInUser,
|
|
246
|
+
);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
console.error(
|
|
249
|
+
'Failed to log stage group completion:',
|
|
250
|
+
error?.message || error,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Close current stage
|
|
256
|
+
currentStage.end_date = now;
|
|
257
|
+
currentStage.is_current = 'N';
|
|
258
|
+
await this.stageMovementRepo.save(currentStage);
|
|
259
|
+
|
|
260
|
+
// Insert next stage
|
|
261
|
+
await this.stageMovementRepo.save({
|
|
262
|
+
entity_type: 'WFSA',
|
|
263
|
+
mapped_entity_type,
|
|
264
|
+
mapped_entity_id,
|
|
265
|
+
name: nextStage.name,
|
|
266
|
+
status: nextStage.status,
|
|
267
|
+
current_user_id: loggedInUser.id,
|
|
268
|
+
stage_action_mapping_id: nextStage.id,
|
|
269
|
+
organization_id: loggedInUser.organization_id,
|
|
270
|
+
start_date: now,
|
|
271
|
+
stage_group_id: nextStage?.stage_group_id,
|
|
272
|
+
stage_id: nextStage.id,
|
|
273
|
+
is_current: 'Y',
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
await this.populateActionService(
|
|
277
|
+
stageData.nextStage.id,
|
|
278
|
+
loggedInUser,
|
|
279
|
+
mapped_entity_id,
|
|
280
|
+
mapped_entity_type,
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
// Stage completion log
|
|
284
|
+
try {
|
|
285
|
+
await this.activityLogService.logActivity(
|
|
286
|
+
{
|
|
287
|
+
mapped_entity_id: mapped_entity_id,
|
|
288
|
+
mapped_entity_type: mapped_entity_type,
|
|
289
|
+
title: 'Stage Completed',
|
|
290
|
+
description: `Stage changed from ${currentStage.name} to ${nextStage.name}.`,
|
|
291
|
+
action: 'status',
|
|
292
|
+
category: ACTIVITY_CATEGORIES.STAGE,
|
|
293
|
+
appcode: loggedInUser.appcode,
|
|
294
|
+
},
|
|
295
|
+
loggedInUser,
|
|
296
|
+
);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
console.error(
|
|
299
|
+
'Failed to log activity for stage completion:',
|
|
300
|
+
error?.message || error,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return `Moved to next stage (Stage ID: ${nextStage.id}).`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Case 3: No next stage – mark workflow as done
|
|
307
|
+
currentStage.end_date = now;
|
|
308
|
+
currentStage.is_current = 'N';
|
|
309
|
+
await this.stageMovementRepo.save(currentStage);
|
|
310
|
+
|
|
311
|
+
return 'Workflow completed. No next stage available.';
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async populateActionService(
|
|
315
|
+
stage_id: number,
|
|
316
|
+
loggedInUser: UserData,
|
|
317
|
+
mapped_entity_id: number,
|
|
318
|
+
mapped_entity_type: string,
|
|
319
|
+
): Promise<any> {
|
|
320
|
+
const actions =
|
|
321
|
+
await this.stageMovementRepository.getAllActionByStageId(stage_id);
|
|
322
|
+
// Populate the action service with the retrieved actions
|
|
323
|
+
|
|
324
|
+
if (!actions || actions.length === 0) {
|
|
325
|
+
return 'No actions found for this stage.';
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// check whether first action's action_category is owner_assignment and assignment_type is AUTO_ASSIGN
|
|
329
|
+
const firstAction = actions[0];
|
|
330
|
+
const actionCategory = await this.dataSource.query(
|
|
331
|
+
`SELECT code FROM frm_wf_action_category WHERE id = ${Number(firstAction.action_category)}`,
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
const assignmentType = await this.dataSource.query(
|
|
335
|
+
`SELECT value FROM frm_list_master_items WHERE id = ${Number(firstAction.assignment_type)}`,
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
// save in action data
|
|
339
|
+
await this.actionDataService.saveActionData(
|
|
340
|
+
actions,
|
|
341
|
+
loggedInUser,
|
|
342
|
+
mapped_entity_id,
|
|
343
|
+
mapped_entity_type,
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
// save in task data
|
|
347
|
+
await this.taskService.saveActionData(
|
|
348
|
+
actions,
|
|
349
|
+
loggedInUser,
|
|
350
|
+
mapped_entity_id,
|
|
351
|
+
mapped_entity_type,
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
if (
|
|
355
|
+
actionCategory[0]?.code == 'OWAS' &&
|
|
356
|
+
assignmentType[0]?.value == 'round_robin'
|
|
357
|
+
) {
|
|
358
|
+
console.log('Auto-assigning owner based on round-robin assignment type');
|
|
359
|
+
await this.assignLead(
|
|
360
|
+
loggedInUser,
|
|
361
|
+
mapped_entity_id,
|
|
362
|
+
mapped_entity_type,
|
|
363
|
+
stage_id,
|
|
364
|
+
actions,
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async updateLeadOwner(entityData, loggedInUser) {
|
|
370
|
+
const { lead_id, lead_owner, stage_id, entity_type } = entityData;
|
|
371
|
+
const updatedData = {
|
|
372
|
+
id: lead_id,
|
|
373
|
+
lead_owner: lead_owner,
|
|
374
|
+
entity_type: entity_type,
|
|
375
|
+
};
|
|
376
|
+
entityData = updatedData;
|
|
377
|
+
|
|
378
|
+
this.modificationService.logModification(
|
|
379
|
+
{
|
|
380
|
+
entity_type: 'ENMD',
|
|
381
|
+
mapped_entity_type: entity_type,
|
|
382
|
+
mapped_entity_id: lead_id,
|
|
383
|
+
attribute_key: 'lead_owner',
|
|
384
|
+
new_value: lead_owner,
|
|
385
|
+
old_value: entityData.lead_owner,
|
|
386
|
+
remarks: entityData.remarks,
|
|
387
|
+
reason_code: entityData.reason_code,
|
|
388
|
+
stage_id: stage_id,
|
|
389
|
+
action_id: entityData.action_id,
|
|
390
|
+
},
|
|
391
|
+
loggedInUser,
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
const leadData: any = await this.getEntityData(
|
|
395
|
+
'LEAD',
|
|
396
|
+
lead_id,
|
|
397
|
+
loggedInUser,
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
const unassignedListMasterItemData = await this.dataSource.query(
|
|
401
|
+
`SELECT name, id
|
|
402
|
+
FROM frm_list_master_items
|
|
403
|
+
WHERE listtype = "LEST" AND organization_id = $1
|
|
404
|
+
AND value IN ($2, $3)`,
|
|
405
|
+
[loggedInUser.organization_id, 'unassigned', 'active'],
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
// Find the IDs explicitly
|
|
409
|
+
const unassignedId = unassignedListMasterItemData.find(
|
|
410
|
+
(item) => item.name.toLowerCase() === 'unassigned',
|
|
411
|
+
)?.id;
|
|
412
|
+
|
|
413
|
+
const activeId = unassignedListMasterItemData.find(
|
|
414
|
+
(item) => item.name.toLowerCase() === 'active',
|
|
415
|
+
)?.id;
|
|
416
|
+
|
|
417
|
+
// Use unassignedId for comparison
|
|
418
|
+
if (leadData?.lead_status === unassignedId) {
|
|
419
|
+
leadData.lead_status = activeId;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const result = await super.updateEntity(
|
|
423
|
+
{
|
|
424
|
+
...entityData,
|
|
425
|
+
status: leadData?.lead_status,
|
|
426
|
+
lead_status: leadData?.lead_status,
|
|
427
|
+
},
|
|
428
|
+
loggedInUser,
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
await this.dataSource.query(
|
|
432
|
+
`UPDATE frm_wf_action_data SET user_id =$1
|
|
433
|
+
WHERE mapped_entity_id=$2
|
|
434
|
+
AND mapped_entity_type = $3
|
|
435
|
+
AND stage_id = $4
|
|
436
|
+
AND (is_current = 'Y' OR is_current IS NULL)
|
|
437
|
+
`,
|
|
438
|
+
[lead_owner, lead_id, entity_type, stage_id],
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
await this.dataSource.query(
|
|
442
|
+
`UPDATE crm_lead_meeting SET user_id=$1
|
|
443
|
+
WHERE stage_id = $2
|
|
444
|
+
AND mapped_entity_id = $3
|
|
445
|
+
AND mapped_entity_type = "LEAD"
|
|
446
|
+
AND (status='scheduled' OR status='rescheduled')
|
|
447
|
+
`,
|
|
448
|
+
[lead_owner, stage_id, lead_id],
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
const taskRows = await this.dataSource.query(
|
|
452
|
+
`SELECT id,status FROM frm_wf_task_data
|
|
453
|
+
WHERE mapped_entity_id = $1
|
|
454
|
+
AND mapped_entity_type = $2
|
|
455
|
+
AND stage_id = $3`,
|
|
456
|
+
[lead_id, entity_type, stage_id],
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
for (const task of taskRows) {
|
|
460
|
+
const statusRows = await this.dataSource.query(
|
|
461
|
+
`SELECT value FROM frm_list_master_items
|
|
462
|
+
WHERE id = $1 AND organization_id = $2`,
|
|
463
|
+
[task.status, loggedInUser.organization_id],
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
const statusName = statusRows[0]?.value?.toLowerCase() || '';
|
|
467
|
+
if (
|
|
468
|
+
['todo', 'in_progress'].includes(statusName) ||
|
|
469
|
+
statusName === 'todo'
|
|
470
|
+
) {
|
|
471
|
+
await this.dataSource.query(
|
|
472
|
+
`UPDATE frm_wf_task_data SET user_id = $1, task_owner = $2 WHERE id = $3`,
|
|
473
|
+
[lead_owner, lead_owner, task.id],
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
let leadOwnerName;
|
|
479
|
+
|
|
480
|
+
try {
|
|
481
|
+
const baseUrl = this.configService.get<string>('REDIRECT_BE_URL');
|
|
482
|
+
|
|
483
|
+
// Prepare query params
|
|
484
|
+
const queryParams = new URLSearchParams({
|
|
485
|
+
loggedInUser: JSON.stringify(loggedInUser),
|
|
486
|
+
}).toString();
|
|
487
|
+
|
|
488
|
+
const url = `${baseUrl}/entity/public/getById/${entityData?.lead_owner}?entity_type=USR&${queryParams}`;
|
|
489
|
+
|
|
490
|
+
const response = await axios.get(url);
|
|
491
|
+
|
|
492
|
+
leadOwnerName = response.data;
|
|
493
|
+
} catch (error) {
|
|
494
|
+
console.error('Internal Entity API call failed:', error.message);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
try {
|
|
498
|
+
const logData = {
|
|
499
|
+
mapped_entity_id: lead_id,
|
|
500
|
+
mapped_entity_type: 'LEAD',
|
|
501
|
+
title: 'Owner Assigned',
|
|
502
|
+
description: leadData?.lead_owner
|
|
503
|
+
? `${leadOwnerName?.name} reassigned as Lead owner.`
|
|
504
|
+
: `${leadOwnerName?.name} assigned as Lead owner.`,
|
|
505
|
+
action: 'assign',
|
|
506
|
+
category: ACTIVITY_CATEGORIES.ASSIGN,
|
|
507
|
+
appcode: loggedInUser.appcode,
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
await this.activityLogService.logActivity(logData, loggedInUser);
|
|
511
|
+
} catch (error) {
|
|
512
|
+
console.error(
|
|
513
|
+
'Failed to log activity for meeting:',
|
|
514
|
+
error?.message || error,
|
|
515
|
+
);
|
|
516
|
+
// Logging should not block main flow
|
|
517
|
+
}
|
|
518
|
+
return result;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async assignLead(
|
|
522
|
+
loggedInUser: UserData,
|
|
523
|
+
mapped_entity_id: number,
|
|
524
|
+
mapped_entity_type: string,
|
|
525
|
+
stage_id: number,
|
|
526
|
+
actions: any[],
|
|
527
|
+
): Promise<number> {
|
|
528
|
+
const { organization_id, level_id, level_type } = loggedInUser;
|
|
529
|
+
|
|
530
|
+
// 1) Get eligible owners (distinct + stable ordering) // axios call
|
|
531
|
+
// const owners: Array<{ id: number }> = await this.dataSource.query(
|
|
532
|
+
// `
|
|
533
|
+
// SELECT DISTINCT u.id
|
|
534
|
+
// FROM sso_user u
|
|
535
|
+
// JOIN sso_user_role_mapping urm ON u.id = urm.user_id
|
|
536
|
+
// JOIN sso_role r ON urm.role_id = r.id
|
|
537
|
+
// JOIN sso_module_access ma ON ma.role_code = r.code
|
|
538
|
+
// WHERE urm.organization_id = ?
|
|
539
|
+
// AND urm.level_id = ?
|
|
540
|
+
// AND urm.level_type = ?
|
|
541
|
+
// AND urm.appcode = 'CRM'
|
|
542
|
+
// AND ma.appcode = 'CRM'
|
|
543
|
+
// AND ma.module_code = 'lead_crm_sch'
|
|
544
|
+
// AND ma.access_flag = 1
|
|
545
|
+
// AND ma.action_type = 'LEAD_OWNER'
|
|
546
|
+
// ORDER BY u.id ASC
|
|
547
|
+
// `,
|
|
548
|
+
// [organization_id, Number(level_id), level_type],
|
|
549
|
+
// );
|
|
550
|
+
|
|
551
|
+
let owners = [] as any;
|
|
552
|
+
|
|
553
|
+
try {
|
|
554
|
+
const baseUrl = this.configService.get<string>('REDIRECT_BE_URL');
|
|
555
|
+
|
|
556
|
+
// Prepare query params
|
|
557
|
+
const queryParams = new URLSearchParams({
|
|
558
|
+
loggedInUser: JSON.stringify(loggedInUser),
|
|
559
|
+
}).toString();
|
|
560
|
+
|
|
561
|
+
const { level_id } = loggedInUser;
|
|
562
|
+
|
|
563
|
+
const url = `${baseUrl}/users/public/lead-owners/?entity_type=USR&${queryParams}`;
|
|
564
|
+
|
|
565
|
+
const response = await axios.get(url);
|
|
566
|
+
|
|
567
|
+
owners = response.data;
|
|
568
|
+
|
|
569
|
+
// The API response is likely a JSON object, not an array
|
|
570
|
+
} catch (error) {
|
|
571
|
+
console.error('Internal Entity API call failed:', error.message);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (!owners?.length)
|
|
575
|
+
console.log('No eligible owners found for lead assignment.');
|
|
576
|
+
const userIds = owners?.map((o) => Number(o.id)); // normalize to numbers
|
|
577
|
+
|
|
578
|
+
// 2) Find the last assigned *eligible* owner (use IN (...))
|
|
579
|
+
const placeholders = userIds.map(() => '?').join(',');
|
|
580
|
+
const lastRow: Array<{ lead_owner: number }> = await this.dataSource.query(
|
|
581
|
+
`
|
|
582
|
+
SELECT lead_owner
|
|
583
|
+
FROM crm_lead
|
|
584
|
+
WHERE organization_id = $1
|
|
585
|
+
AND level_id = $2
|
|
586
|
+
AND level_type = $3
|
|
587
|
+
AND lead_owner IN (${placeholders})
|
|
588
|
+
ORDER BY created_date DESC
|
|
589
|
+
LIMIT 1
|
|
590
|
+
`,
|
|
591
|
+
[organization_id, Number(level_id), level_type, ...userIds],
|
|
592
|
+
);
|
|
593
|
+
|
|
594
|
+
const lastAssigned = lastRow.length ? Number(lastRow[0].lead_owner) : null;
|
|
595
|
+
|
|
596
|
+
// 3) Compute next user in round-robin
|
|
597
|
+
const lastIdx = lastAssigned != null ? userIds.indexOf(lastAssigned) : -1;
|
|
598
|
+
const nextIdx = (lastIdx + 1) % userIds.length;
|
|
599
|
+
const nextUser = userIds[nextIdx];
|
|
600
|
+
|
|
601
|
+
//4) Update lead owner in the lead table no need because we are updating lead owner in updateLeadOwner method
|
|
602
|
+
// await this.dataSource.query(
|
|
603
|
+
// `
|
|
604
|
+
// UPDATE crm_lead
|
|
605
|
+
// SET lead_owner = ?
|
|
606
|
+
// WHERE organization_id = ?
|
|
607
|
+
// AND level_id = ?
|
|
608
|
+
// AND level_type = ?
|
|
609
|
+
// AND lead_owner IS NULL
|
|
610
|
+
// ORDER BY created_date ASC
|
|
611
|
+
// LIMIT 1
|
|
612
|
+
// `,
|
|
613
|
+
// [nextUser, organization_id, Number(level_id), level_type],
|
|
614
|
+
// );
|
|
615
|
+
|
|
616
|
+
console.log(`Assigning lead to user ID: ${nextUser}`);
|
|
617
|
+
|
|
618
|
+
const firstAction = actions[0];
|
|
619
|
+
|
|
620
|
+
// update lead owner status
|
|
621
|
+
await this.updateLeadOwner(
|
|
622
|
+
{
|
|
623
|
+
lead_id: mapped_entity_id,
|
|
624
|
+
lead_owner: nextUser,
|
|
625
|
+
stage_id: stage_id,
|
|
626
|
+
entity_type: mapped_entity_type,
|
|
627
|
+
action_id: firstAction.id,
|
|
628
|
+
},
|
|
629
|
+
loggedInUser,
|
|
630
|
+
);
|
|
631
|
+
|
|
632
|
+
// move task
|
|
633
|
+
await this.taskService.moveTask(loggedInUser, {
|
|
634
|
+
mapped_entity_type,
|
|
635
|
+
mapped_entity_id,
|
|
636
|
+
stage_id,
|
|
637
|
+
action_id: firstAction.id,
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
// if it has only one action then move next stage
|
|
641
|
+
if (actions.length == 1) {
|
|
642
|
+
console.log(
|
|
643
|
+
'Only one action present and it is owner assignment. Moving to next stage.',
|
|
644
|
+
);
|
|
645
|
+
await this.moveToNextStage(
|
|
646
|
+
mapped_entity_type,
|
|
647
|
+
mapped_entity_id,
|
|
648
|
+
loggedInUser,
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
return nextUser;
|
|
653
|
+
}
|
|
654
|
+
}
|