|
1 | 1 | package org.avni.messaging.api;
|
2 | 2 |
|
| 3 | +import org.avni.messaging.contract.ManualMessageContract; |
3 | 4 | import org.avni.messaging.contract.glific.GlificMessageTemplate;
|
4 | 5 | import org.avni.messaging.contract.web.MessageRequestResponse;
|
5 | 6 | import org.avni.messaging.domain.MessageDeliveryStatus;
|
6 | 7 | import org.avni.messaging.domain.MessageRequest;
|
7 | 8 | import org.avni.messaging.domain.ReceiverType;
|
| 9 | +import org.avni.messaging.domain.exception.GlificNotConfiguredException; |
8 | 10 | import org.avni.messaging.service.MessageRequestService;
|
9 | 11 | import org.avni.messaging.service.MessageTemplateService;
|
| 12 | +import org.avni.messaging.service.MessagingService; |
| 13 | +import org.avni.messaging.service.PhoneNumberNotAvailableOrIncorrectException; |
10 | 14 | import org.avni.server.dao.UserRepository;
|
| 15 | +import org.avni.server.domain.accessControl.PrivilegeType; |
11 | 16 | import org.avni.server.service.IndividualService;
|
| 17 | +import org.avni.server.service.accessControl.AccessControlService; |
12 | 18 | import org.springframework.beans.factory.annotation.Autowired;
|
13 | 19 | import org.springframework.http.ResponseEntity;
|
14 | 20 | import org.springframework.security.access.prepost.PreAuthorize;
|
15 | 21 | import org.springframework.transaction.annotation.Transactional;
|
16 |
| -import org.springframework.web.bind.annotation.PathVariable; |
17 |
| -import org.springframework.web.bind.annotation.RequestMapping; |
18 |
| -import org.springframework.web.bind.annotation.RequestMethod; |
19 |
| -import org.springframework.web.bind.annotation.RestController; |
| 22 | +import org.springframework.web.bind.annotation.*; |
20 | 23 |
|
21 | 24 | import java.util.List;
|
22 | 25 | import java.util.stream.Collectors;
|
|
25 | 28 | @RestController
|
26 | 29 | public class MessageController {
|
27 | 30 | private static final String MessageEndpoint = "/web/message";
|
| 31 | + private final AccessControlService accessControlService; |
28 | 32 | private final MessageRequestService messageRequestService;
|
| 33 | + private final MessagingService messagingService; |
29 | 34 | private final UserRepository userRepository;
|
30 | 35 | private final IndividualService individualService;
|
31 | 36 | private final MessageTemplateService messageTemplateService;
|
32 | 37 |
|
33 | 38 | @Autowired
|
34 |
| - public MessageController(MessageRequestService messageRequestService, UserRepository userRepository, |
| 39 | + public MessageController(AccessControlService accessControlService, MessageRequestService messageRequestService, MessagingService messagingService, UserRepository userRepository, |
35 | 40 | IndividualService individualService, MessageTemplateService messageTemplateService) {
|
| 41 | + this.accessControlService = accessControlService; |
36 | 42 | this.messageRequestService = messageRequestService;
|
| 43 | + this.messagingService = messagingService; |
37 | 44 | this.userRepository = userRepository;
|
38 | 45 | this.individualService = individualService;
|
39 | 46 | this.messageTemplateService = messageTemplateService;
|
@@ -61,6 +68,23 @@ public ResponseEntity<List<MessageRequestResponse>> fetchAllMsgsNotYetSentForCon
|
61 | 68 | .collect(Collectors.toList()));
|
62 | 69 | }
|
63 | 70 |
|
| 71 | + @RequestMapping(value = MessageEndpoint + "/sendMsg", method = RequestMethod.POST) |
| 72 | + @PreAuthorize(value = "hasAnyAuthority('user')") |
| 73 | + @Transactional |
| 74 | + public ResponseEntity<MessageDeliveryStatus> sendMsgToContactUser(@RequestBody ManualMessageContract manualMessageContract) { |
| 75 | + accessControlService.checkPrivilege(PrivilegeType.Messaging); |
| 76 | + accessControlService.checkPrivilege(PrivilegeType.EditUserConfiguration); |
| 77 | + // TODO: 25/12/24 Return exception message as well in response to enable reporting insight into the issue |
| 78 | + try { |
| 79 | + messagingService.sendMessageSynchronously(manualMessageContract); |
| 80 | + } catch (GlificNotConfiguredException | PhoneNumberNotAvailableOrIncorrectException e) { |
| 81 | + return ResponseEntity.badRequest().body(MessageDeliveryStatus.NotSent); |
| 82 | + } catch (Exception e) { |
| 83 | + return ResponseEntity.internalServerError().body(MessageDeliveryStatus.Failed); |
| 84 | + } |
| 85 | + return ResponseEntity.ok(MessageDeliveryStatus.Sent); |
| 86 | + } |
| 87 | + |
64 | 88 | @RequestMapping(value = MessageEndpoint + "/contactGroup/{id}/msgsNotYetSent", method = RequestMethod.GET)
|
65 | 89 | @PreAuthorize(value = "hasAnyAuthority('user')")
|
66 | 90 | @Transactional(readOnly = true)
|
|
0 commit comments