ToUpsertParams converts PhoneUpsert to services.PhoneUpsertParams. The body parameter is the raw JSON request body used to detect which fields were explicitly sent.
(user entities.AuthContext, source string, body []byte)
| 49 | // ToUpsertParams converts PhoneUpsert to services.PhoneUpsertParams. |
| 50 | // The body parameter is the raw JSON request body used to detect which fields were explicitly sent. |
| 51 | func (input *PhoneUpsert) ToUpsertParams(user entities.AuthContext, source string, body []byte) *services.PhoneUpsertParams { |
| 52 | phone, _ := phonenumbers.Parse(input.PhoneNumber, phonenumbers.UNKNOWN_REGION) |
| 53 | |
| 54 | fields := make(map[string]json.RawMessage) |
| 55 | _ = json.Unmarshal(body, &fields) |
| 56 | |
| 57 | var messagesPerMinute *uint |
| 58 | if _, exists := fields["messages_per_minute"]; exists { |
| 59 | messagesPerMinute = &input.MessagesPerMinute |
| 60 | } |
| 61 | |
| 62 | var fcmToken *string |
| 63 | if _, exists := fields["fcm_token"]; exists { |
| 64 | fcmToken = &input.FcmToken |
| 65 | } |
| 66 | |
| 67 | var timeout *time.Duration |
| 68 | if _, exists := fields["message_expiration_seconds"]; exists { |
| 69 | duration := time.Duration(input.MessageExpirationSeconds) * time.Second |
| 70 | timeout = &duration |
| 71 | } |
| 72 | |
| 73 | var maxSendAttempts *uint |
| 74 | if _, exists := fields["max_send_attempts"]; exists { |
| 75 | maxSendAttempts = &input.MaxSendAttempts |
| 76 | } |
| 77 | |
| 78 | var scheduleID *uuid.UUID |
| 79 | if _, exists := fields["message_send_schedule_id"]; exists { |
| 80 | if parsed, err := uuid.Parse(strings.TrimSpace(input.MessageSendScheduleID)); err == nil { |
| 81 | scheduleID = &parsed |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return &services.PhoneUpsertParams{ |
| 86 | Source: source, |
| 87 | PhoneNumber: phone, |
| 88 | MessagesPerMinute: messagesPerMinute, |
| 89 | MissedCallAutoReply: input.MissedCallAutoReply, |
| 90 | MessageExpirationDuration: timeout, |
| 91 | MaxSendAttempts: maxSendAttempts, |
| 92 | FcmToken: fcmToken, |
| 93 | UserID: user.ID, |
| 94 | SIM: entities.SIM(input.SIM), |
| 95 | MessageSendScheduleID: scheduleID, |
| 96 | } |
| 97 | } |