VendorInfluencerTerminate.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Controllers;
  3. use App\Controllers\BaseController;
  4. use App\Models\VendorInfluencerMappingModel;
  5. use App\Models\UserModel;
  6. use CodeIgniter\HTTP\ResponseInterface;
  7. /**
  8. * 벤더-인플루언서 파트너십 해지 API 예제
  9. * 경로: POST /api/vendor-influencer/terminate
  10. */
  11. class VendorInfluencerTerminate extends BaseController
  12. {
  13. protected $vendorInfluencerModel;
  14. protected $userModel;
  15. public function __construct()
  16. {
  17. $this->vendorInfluencerModel = new VendorInfluencerMappingModel();
  18. $this->userModel = new UserModel();
  19. }
  20. /**
  21. * 승인된 파트너십 해지 처리
  22. */
  23. public function terminate()
  24. {
  25. try {
  26. $request = $this->request->getJSON();
  27. $mappingSeq = $request->mappingSeq ?? null;
  28. $terminateReason = $request->terminateReason ?? null;
  29. $terminatedBy = $request->terminatedBy ?? null;
  30. // 필수 파라미터 검증
  31. if (!$mappingSeq || !$terminateReason || !$terminatedBy) {
  32. return $this->response->setStatusCode(400)->setJSON([
  33. 'success' => false,
  34. 'message' => '필수 파라미터가 누락되었습니다. (mappingSeq, terminateReason, terminatedBy 필요)'
  35. ]);
  36. }
  37. // 해지 사유 길이 검증
  38. if (strlen($terminateReason) > 500) {
  39. return $this->response->setStatusCode(400)->setJSON([
  40. 'success' => false,
  41. 'message' => '해지 사유는 500자를 초과할 수 없습니다.'
  42. ]);
  43. }
  44. // 기존 매핑 확인 (승인된 상태여야 함)
  45. $existingMapping = $this->vendorInfluencerModel
  46. ->where('SEQ', $mappingSeq)
  47. ->where('STATUS', 'APPROVED')
  48. ->where('IS_ACT', 'Y')
  49. ->first();
  50. if (!$existingMapping) {
  51. return $this->response->setStatusCode(404)->setJSON([
  52. 'success' => false,
  53. 'message' => '해지할 수 있는 승인된 파트너십을 찾을 수 없습니다.'
  54. ]);
  55. }
  56. // 해지 권한 확인 (벤더사 또는 관련 사용자만 해지 가능)
  57. $terminatingUser = $this->userModel
  58. ->where('SEQ', $terminatedBy)
  59. ->where('IS_ACT', 'Y')
  60. ->first();
  61. if (!$terminatingUser) {
  62. return $this->response->setStatusCode(400)->setJSON([
  63. 'success' => false,
  64. 'message' => '해지 처리자 정보를 찾을 수 없습니다.'
  65. ]);
  66. }
  67. // 해지 처리 데이터 준비
  68. $terminateData = [
  69. 'STATUS' => 'TERMINATED',
  70. 'RESPONSE_MESSAGE' => '파트너십 해지: ' . $terminateReason,
  71. 'RESPONSE_DATE' => date('Y-m-d H:i:s'),
  72. 'APPROVED_BY' => $terminatedBy, // 해지 처리자
  73. 'PARTNERSHIP_END_DATE' => date('Y-m-d H:i:s'), // 파트너십 종료일
  74. 'MOD_DATE' => date('Y-m-d H:i:s')
  75. ];
  76. log_message('info', "파트너십 해지 처리 시작 - 매핑 SEQ: {$mappingSeq}, 해지자: {$terminatedBy}");
  77. // 해지 처리 실행
  78. $result = $this->vendorInfluencerModel->update($mappingSeq, $terminateData);
  79. if (!$result) {
  80. log_message('error', "파트너십 해지 업데이트 실패 - 매핑 SEQ: {$mappingSeq}");
  81. return $this->response->setStatusCode(500)->setJSON([
  82. 'success' => false,
  83. 'message' => '파트너십 해지 처리 중 데이터베이스 오류가 발생했습니다.'
  84. ]);
  85. }
  86. // 해지된 매핑 정보 조회
  87. $terminatedMapping = $this->vendorInfluencerModel
  88. ->select('vim.SEQ, vim.VENDOR_SEQ, vim.INFLUENCER_SEQ, vim.STATUS,
  89. vim.RESPONSE_MESSAGE, vim.RESPONSE_DATE, vim.PARTNERSHIP_END_DATE,
  90. v.COMPANY_NAME as vendorName,
  91. inf.NICK_NAME as influencerNickname, inf.NAME as influencerName')
  92. ->from('VENDOR_INFLUENCER_MAPPING vim')
  93. ->join('VENDOR_LIST v', 'vim.VENDOR_SEQ = v.SEQ', 'left')
  94. ->join('USER_LIST inf', 'vim.INFLUENCER_SEQ = inf.SEQ', 'left')
  95. ->where('vim.SEQ', $mappingSeq)
  96. ->get()
  97. ->getRowArray();
  98. log_message('info', "파트너십 해지 완료 - 매핑 SEQ: {$mappingSeq}");
  99. return $this->response->setJSON([
  100. 'success' => true,
  101. 'message' => '파트너십이 성공적으로 해지되었습니다.',
  102. 'data' => [
  103. 'terminatedMapping' => $terminatedMapping,
  104. 'terminateDate' => date('Y-m-d H:i:s'),
  105. 'terminatedBy' => $terminatingUser['NICK_NAME'] ?? $terminatingUser['NAME']
  106. ]
  107. ]);
  108. } catch (\Exception $e) {
  109. log_message('error', "파트너십 해지 처리 중 예외 발생: " . $e->getMessage());
  110. return $this->response->setStatusCode(500)->setJSON([
  111. 'success' => false,
  112. 'message' => '파트너십 해지 처리 중 오류가 발생했습니다.',
  113. 'error' => ENVIRONMENT === 'development' ? $e->getMessage() : null
  114. ]);
  115. }
  116. }
  117. }
  118. /**
  119. * 라우터 설정 예제 (routes.php에 추가)
  120. *
  121. * $routes->group('api/vendor-influencer', ['namespace' => 'App\Controllers'], function($routes) {
  122. * $routes->post('terminate', 'VendorInfluencerController::terminate');
  123. * });
  124. */
  125. /**
  126. * 프론트엔드에서 호출 예제
  127. *
  128. * const params = {
  129. * mappingSeq: 123,
  130. * terminateReason: "계약 조건 위반으로 인한 해지",
  131. * terminatedBy: 8 // 해지 처리자 USER SEQ
  132. * };
  133. *
  134. * useAxios()
  135. * .post('/api/vendor-influencer/terminate', params)
  136. * .then((res) => {
  137. * if (res.data.success) {
  138. * console.log('해지 완료:', res.data.data);
  139. * // 성공 처리
  140. * } else {
  141. * console.error('해지 실패:', res.data.message);
  142. * // 실패 처리
  143. * }
  144. * })
  145. * .catch((err) => {
  146. * console.error('해지 오류:', err);
  147. * });
  148. */
  149. /**
  150. * 응답 예제
  151. *
  152. * 성공시:
  153. * {
  154. * "success": true,
  155. * "message": "파트너십이 성공적으로 해지되었습니다.",
  156. * "data": {
  157. * "terminatedMapping": {
  158. * "SEQ": 123,
  159. * "VENDOR_SEQ": 8,
  160. * "INFLUENCER_SEQ": 23,
  161. * "STATUS": "TERMINATED",
  162. * "RESPONSE_MESSAGE": "파트너십 해지: 계약 조건 위반으로 인한 해지",
  163. * "RESPONSE_DATE": "2025-07-23 10:30:00",
  164. * "PARTNERSHIP_END_DATE": "2025-07-23 10:30:00",
  165. * "vendorName": "테스트 벤더사",
  166. * "influencerNickname": "인플루언서닉네임",
  167. * "influencerName": "인플루언서이름"
  168. * },
  169. * "terminateDate": "2025-07-23 10:30:00",
  170. * "terminatedBy": "벤더관리자"
  171. * }
  172. * }
  173. *
  174. * 실패시:
  175. * {
  176. * "success": false,
  177. * "message": "해지할 수 있는 승인된 파트너십을 찾을 수 없습니다."
  178. * }
  179. */