guesty-client 0.1.0 → 0.2.0

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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +59 -1
  3. data/README.md +2 -3
  4. data/lib/guesty/access_tokens/upsert.rb +8 -34
  5. data/lib/guesty/api_error.rb +8 -16
  6. data/lib/guesty/client.rb +6 -12
  7. data/lib/guesty/config.rb +1 -16
  8. data/lib/guesty/conversations/attachments/index.rb +2 -6
  9. data/lib/guesty/conversations/index.rb +14 -32
  10. data/lib/guesty/conversations/messages/reply.rb +2 -24
  11. data/lib/guesty/conversations/posts/index.rb +5 -22
  12. data/lib/guesty/conversations/posts/since.rb +0 -1
  13. data/lib/guesty/conversations/search.rb +2 -5
  14. data/lib/guesty/conversations/show.rb +1 -10
  15. data/lib/guesty/conversations.rb +5 -1
  16. data/lib/guesty/error.rb +0 -4
  17. data/lib/guesty/guest_folio/invoice_items/index.rb +2 -6
  18. data/lib/guesty/guests/profiles/create.rb +1 -5
  19. data/lib/guesty/guests/profiles/show.rb +1 -8
  20. data/lib/guesty/guests/profiles/update.rb +2 -7
  21. data/lib/guesty/homes/calendars/show.rb +29 -68
  22. data/lib/guesty/homes/search.rb +4 -23
  23. data/lib/guesty/memory_rate_limiter.rb +0 -3
  24. data/lib/guesty/memory_token_store.rb +0 -8
  25. data/lib/guesty/operation.rb +5 -3
  26. data/lib/guesty/payments/index.rb +14 -0
  27. data/lib/guesty/payments/record.rb +59 -0
  28. data/lib/guesty/rate_limiter_middleware.rb +2 -15
  29. data/lib/guesty/reservations/accommodation_fare/update.rb +4 -5
  30. data/lib/guesty/reservations/approve.rb +2 -8
  31. data/lib/guesty/reservations/booking_date/update.rb +19 -0
  32. data/lib/guesty/reservations/confirmation_code/update.rb +22 -0
  33. data/lib/guesty/reservations/create.rb +1 -8
  34. data/lib/guesty/reservations/custom_fields/update.rb +2 -11
  35. data/lib/guesty/reservations/dates/update.rb +2 -8
  36. data/lib/guesty/reservations/decline.rb +1 -11
  37. data/lib/guesty/reservations/guests/update.rb +43 -0
  38. data/lib/guesty/reservations/index.rb +18 -0
  39. data/lib/guesty/reservations/pre_approve.rb +2 -8
  40. data/lib/guesty/reservations/recover.rb +3 -11
  41. data/lib/guesty/reservations/relocate.rb +18 -0
  42. data/lib/guesty/reservations/reviews/extractors/airbnb.rb +0 -1
  43. data/lib/guesty/reservations/reviews/index.rb +17 -42
  44. data/lib/guesty/reservations/search.rb +58 -35
  45. data/lib/guesty/reservations/show.rb +7 -16
  46. data/lib/guesty/reservations/source/update.rb +19 -0
  47. data/lib/guesty/reservations/status/update.rb +1 -3
  48. data/lib/guesty/reviews/respond.rb +1 -3
  49. data/lib/guesty/version.rb +1 -1
  50. data/lib/guesty/views/create.rb +1 -5
  51. data/lib/guesty/views/update.rb +1 -19
  52. data/lib/guesty/webhooks/create.rb +1 -7
  53. data/lib/guesty/webhooks/index.rb +1 -7
  54. data/lib/guesty/webhooks/signatures/verify.rb +3 -6
  55. data/lib/guesty/webhooks/sync.rb +0 -3
  56. data/lib/guesty/webhooks/update.rb +1 -7
  57. data/lib/guesty.rb +8 -4
  58. data/sig/guesty.rbs +52 -30
  59. metadata +9 -5
  60. data/lib/guesty/reservations/flags/update.rb +0 -33
  61. data/lib/guesty/reservations/payments/create.rb +0 -72
  62. data/lib/guesty/reservations/stays/update.rb +0 -42
  63. data/lib/guesty/reservations/update.rb +0 -19
@@ -9,17 +9,12 @@ module Guesty
9
9
  argument! guest_id: :required if guest_id.blank?
10
10
  argument! attributes: :required, fatal: true if attributes.blank?
11
11
 
12
- response = Guesty.do_request :put, "guests-crud/#{guest_id}", prepare_tags(guest_id, attributes)
13
-
14
- raise response.failure unless response.success?
15
-
16
- Success(response.value!.body)
12
+ Success(request(:put, "guests-crud/#{guest_id}", prepare_tags(guest_id, attributes)))
17
13
  end
18
14
 
19
15
  private
20
16
 
21
- # Actions on a single tag are not supported by Guesty.
22
- # Instead, we fetch all tags and upload a modified array.
17
+ # Guesty has no per-tag action, so the whole tag list is read and replaced.
23
18
  def prepare_tags(guest_id, attributes)
24
19
  tags_to_add = attributes[:addTags] || []
25
20
  tags_to_remove = attributes[:removeTags] || []
@@ -25,108 +25,69 @@ module Guesty
25
25
  argument! home_id: :required if home_id.blank?
26
26
  argument! end_date: :invalid if end_date <= start_date
27
27
 
28
- response = fetch_calendar_data(home_id, start_date, end_date)
28
+ body = request(:get, "availability-pricing/api/calendar/listings/minified/#{home_id}", { startDate: start_date, endDate: end_date, view: 'full' })
29
29
 
30
- raise response.failure if response.failure?
31
-
32
- Success(normalize_calendar(response.value!.body))
30
+ Success(normalize_calendar(body.dig(:data, :days) || {}))
33
31
  end
34
32
 
35
33
  private
36
34
 
37
- def fetch_calendar_data(home_id, start_date, end_date)
38
- params = {
39
- startDate: start_date,
40
- endDate: end_date,
41
- view: 'full'
42
- }
43
-
44
- Guesty.do_request :get, "availability-pricing/api/calendar/listings/minified/#{home_id}", params
45
- end
46
-
47
- def normalize_calendar(body)
48
- calendar = body.dig(:data, :days) || {}
35
+ # @rbs (::Hash[::Symbol, untyped] calendar) -> ::Hash[::Symbol, untyped]
36
+ def normalize_calendar(calendar)
49
37
  @blocks = calendar[:blocks] || {}
50
38
  @days = calendar[:calendar] || []
51
- available, unavailable = @days.partition { |day| available_day?(day) }
39
+ validate_currency(calendar[:currency])
52
40
 
53
- validate_currency(calendar[:currency], @days)
41
+ available, unavailable = @days.partition { |day| available_day?(day) }
54
42
 
55
43
  {
56
- available: attrs_for_available(available),
57
- unavailable: attrs_for_unavailable(unavailable),
58
- blocks: attrs_for_blocks
44
+ available: available.map { |day| day_attributes(day, check_in: day[:blocks].empty?) },
45
+ unavailable: unavailable.map { |day| day_attributes(day, check_in: false, blocked_as: day[:blocks].keys.filter_map { BLOCK_TYPE_MAPPING[it] }) },
46
+ blocks: @blocks.values.map { |block| block_attributes(block) }
59
47
  }
60
48
  end
61
49
 
62
- # The host prices in one currency; a listing in another would silently
63
- # misprice. Skipped when the host has not configured one.
64
- # @rbs (::String? currency, ::Array[::Hash[::Symbol, untyped]] days) -> void
65
- def validate_currency(currency, days)
66
- return nil if days.empty? || Guesty.config.default_currency.nil?
67
- return nil if currency.to_s.upcase == Guesty.config.default_currency
50
+ # @rbs (::String? currency) -> void
51
+ def validate_currency(currency)
52
+ return if @days.empty? || Guesty.config.default_currency.nil?
53
+ return if currency.to_s.upcase == Guesty.config.default_currency
68
54
 
69
55
  error! "unsupported currency #{currency}"
70
56
  end
71
57
 
72
58
  # @rbs (::Hash[::Symbol, untyped] day) -> bool
73
59
  def available_day?(day)
74
- day[:blocks].empty? || day_is_available_for_checkout?(day)
60
+ day[:blocks].empty? || available_for_checkout?(day)
75
61
  end
76
62
 
63
+ # A day booked only by a reservation checking in that day still takes a
64
+ # check-out, unless the night before is blocked too.
77
65
  # @rbs (::Hash[::Symbol, untyped] day) -> bool
78
- def day_is_available_for_checkout?(day)
66
+ def available_for_checkout?(day)
79
67
  return false if day[:blocks].except(:b).any?
80
- return false if previous_night_blocked?(day)
68
+ return false if blocked_dates.include?(day[:date].to_date.prev_day.iso8601)
81
69
 
82
70
  day[:blockIds].any? { |id| @blocks.dig(id.to_sym, :reservation, :checkInDateLocalized) == day[:date] }
83
71
  end
84
72
 
85
- # @rbs (::Hash[::Symbol, untyped] day) -> bool
86
- def previous_night_blocked?(day)
87
- blocked_dates.include?(day[:date].to_date.prev_day.iso8601)
88
- end
89
-
90
73
  # @rbs () -> ::Set[::String]
91
74
  def blocked_dates
92
75
  @blocked_dates ||= @days.reject { |day| day[:blocks].empty? }.to_set { |day| day[:date] }
93
76
  end
94
77
 
95
- # @rbs (::Array[::Hash[::Symbol, untyped]] days) -> ::Array[::Hash[::Symbol, untyped]]
96
- def attrs_for_available(days)
97
- days.map do |day|
98
- {
99
- date: day[:date],
100
- price: day[:price],
101
- min_nights: day[:minNights],
102
- check_in: day[:blocks].empty?
103
- }
104
- end
78
+ # @rbs (::Hash[::Symbol, untyped] day, **untyped) -> ::Hash[::Symbol, untyped]
79
+ def day_attributes(day, **attributes)
80
+ { date: day[:date], price: day[:price], min_nights: day[:minNights], **attributes }
105
81
  end
106
82
 
107
- # @rbs (::Array[::Hash[::Symbol, untyped]] days) -> ::Array[::Hash[::Symbol, untyped]]
108
- def attrs_for_unavailable(days)
109
- days.map do |day|
110
- {
111
- date: day[:date],
112
- price: day[:price],
113
- min_nights: day[:minNights],
114
- check_in: false,
115
- blocked_as: day[:blocks].keys.filter_map { |key| BLOCK_TYPE_MAPPING[key] }
116
- }
117
- end
118
- end
119
-
120
- # @rbs () -> ::Array[::Hash[::Symbol, untyped]]
121
- def attrs_for_blocks
122
- @blocks.values.map do |block|
123
- {
124
- kind: BLOCK_TYPE_MAPPING[block[:type].to_sym],
125
- start_date: block[:startDate].to_date,
126
- end_date: block[:endDate].to_date,
127
- comment: block[:note].presence
128
- }
129
- end
83
+ # @rbs (::Hash[::Symbol, untyped] block) -> ::Hash[::Symbol, untyped]
84
+ def block_attributes(block)
85
+ {
86
+ kind: BLOCK_TYPE_MAPPING[block[:type].to_sym],
87
+ start_date: block[:startDate].to_date,
88
+ end_date: block[:endDate].to_date,
89
+ comment: block[:note].presence
90
+ }
130
91
  end
131
92
  end
132
93
  end
@@ -1,32 +1,13 @@
1
1
  module Guesty
2
2
  module Homes
3
- # `active` is a filter, so all listings take two passes. Tags come back as
4
- # ids on the listing; tag names live on GET listings/tags.
5
3
  class Search
6
4
  include Guesty::Operation
7
5
 
8
- # @example
9
- # attributes: {
10
- # params: { ids: 'guestyID', available: { checkIn: "YYYY-MM-DD", checkOut: "YYYY-MM-DD", minOccupancy: 3 }.to_json }
11
- # filters: [{ operator: '$eq', field: 'type', value: 'MTL_CHILD' }],
12
- # response_keys: %w[
13
- # _id
14
- # nickname
15
- # type
16
- # ]
17
- # }
18
-
19
6
  # @rbs (attributes: ::Hash[::Symbol, untyped], ?skip: ::Integer, ?take: ::Integer) -> Dry::Monads::Result[::Hash[::Symbol, untyped]]
20
7
  def call(attributes:, skip: 0, take: 100)
21
8
  argument! take: :invalid if take.blank? || take <= 0
22
9
 
23
- response = Guesty.do_request :get, 'listings', query(attributes, skip, take)
24
-
25
- raise response.failure unless response.success?
26
-
27
- body = response.value!.body
28
-
29
- Success(body.slice(:results, :count))
10
+ Success(request(:get, 'listings', query(attributes, skip, take)).slice(:results, :count))
30
11
  end
31
12
 
32
13
  private
@@ -34,11 +15,11 @@ module Guesty
34
15
  def query(attributes, skip, take)
35
16
  {
36
17
  active: true,
37
- fields: (attributes[:response_keys] || []).join(' '),
18
+ fields: Array(attributes[:response_keys]).join(' '),
38
19
  limit: take,
39
20
  skip:,
40
- **(attributes[:filters].present? ? { filters: attributes[:filters].to_json } : {}),
41
- **(attributes[:params] || {})
21
+ filters: attributes[:filters].presence&.to_json,
22
+ **attributes.fetch(:params, {})
42
23
  }.compact_blank
43
24
  end
44
25
  end
@@ -1,7 +1,4 @@
1
1
  module Guesty
2
- # The default rate limiter: in-memory fixed windows per key. Per-process only;
3
- # a host running several processes injects a shared backend, such as Redis,
4
- # via Guesty.config.rate_limiter.
5
2
  class MemoryRateLimiter
6
3
  def initialize
7
4
  @windows = {}
@@ -1,8 +1,4 @@
1
1
  module Guesty
2
- # The default token store: keeps one token per credential slot in memory for
3
- # the life of the process. Guesty mints only 5 tokens per client id per 24
4
- # hours, so a multi-process host must inject a shared, persistent store, such
5
- # as a database table behind a cache.
6
2
  class MemoryTokenStore
7
3
  Token = Struct.new(:access_token, :expires_at, keyword_init: true) do
8
4
  def expired?
@@ -25,9 +21,5 @@ module Guesty
25
21
  def save(slot_key:, access_token:, expires_at:)
26
22
  @tokens[slot_key] = Token.new(access_token:, expires_at:)
27
23
  end
28
-
29
- def clear
30
- @tokens = {}
31
- end
32
24
  end
33
25
  end
@@ -1,7 +1,4 @@
1
1
  module Guesty
2
- # Shared monadic contract for the client operations: #call returns a Result,
3
- # #call! unwraps it and raises the underlying failure. An error raised inside
4
- # #call becomes Failure(error), mirroring the host's operation base.
5
2
  module Operation
6
3
  def self.included(base)
7
4
  base.include Dry::Monads[:result]
@@ -25,6 +22,11 @@ module Guesty
25
22
 
26
23
  private
27
24
 
25
+ # @rbs (::Symbol request_method, ::String path, ?::Hash[::Symbol, untyped] params, ?retries: ::Integer, ?timeout: ::Integer) -> untyped
26
+ def request(...)
27
+ Guesty.do_request(...).value_or { |error| raise error }.body
28
+ end
29
+
28
30
  def argument!(fatal: false, **attrs)
29
31
  key, reason = attrs.first
30
32
  raise Guesty::ArgumentError.new("#{key}: #{reason}", fatal:)
@@ -0,0 +1,14 @@
1
+ module Guesty
2
+ module Payments
3
+ class Index
4
+ include Guesty::Operation
5
+
6
+ # @rbs (reservation_id: ::String) -> Dry::Monads::Result[::Hash[::Symbol, untyped]]
7
+ def call(reservation_id:)
8
+ argument! reservation_id: :required if reservation_id.blank?
9
+
10
+ Success(request(:get, 'payments', { reservationId: reservation_id }))
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,59 @@
1
+ module Guesty
2
+ module Payments
3
+ # Guesty answers 503 PAYMENT_RESERVATION_NOT_READY right after the
4
+ # reservation is created; the ApiError is transient, so retry later.
5
+ class Record
6
+ include Guesty::Operation
7
+
8
+ METHODS = %w[RECORDED_CASH ECHECK DEBIT CREDIT BANK_TRANSFER CREDIT_NOTE VOUCHER CHECK OTHER].freeze #: ::Array[::String]
9
+ NOTE_MAX_LENGTH = 500 #: ::Integer
10
+
11
+ # @rbs (reservation_id: ::String, attributes: ::Hash[::Symbol, untyped]) -> Dry::Monads::Result[::Hash[::Symbol, untyped]]
12
+ def call(reservation_id:, attributes:)
13
+ validate_arguments!(reservation_id:, attributes:)
14
+
15
+ Success(request(:post, 'payments/record', payload(reservation_id:, attributes:), retries: 0))
16
+ end
17
+
18
+ private
19
+
20
+ def validate_arguments!(reservation_id:, attributes:)
21
+ argument! reservation_id: :required if reservation_id.blank?
22
+ argument! attributes: :required if attributes.blank?
23
+ argument! amount: :required if attributes[:amount].blank?
24
+ argument! amount: :must_be_positive if attributes[:amount].present? && attributes[:amount] <= 0
25
+ argument! paid_at: :required if attributes[:paid_at].blank?
26
+
27
+ validate_note_and_method!(attributes)
28
+ end
29
+
30
+ # @rbs (::Hash[::Symbol, untyped] attributes) -> void
31
+ def validate_note_and_method!(attributes)
32
+ argument! note: :required if attributes[:note].blank?
33
+ argument! note: :invalid if attributes[:note].present? && attributes[:note].length > NOTE_MAX_LENGTH
34
+
35
+ method = attributes[:method] || METHODS.first
36
+ argument! method: :invalid unless METHODS.include?(method)
37
+ end
38
+
39
+ # @rbs (reservation_id: ::String, attributes: ::Hash[::Symbol, untyped]) -> ::Hash[::Symbol, untyped]
40
+ def payload(reservation_id:, attributes:)
41
+ {
42
+ reservationId: reservation_id,
43
+ amount: attributes[:amount],
44
+ paymentMethod: { method: attributes[:method] || METHODS.first },
45
+ shouldBePaidAt: at(attributes[:paid_at]),
46
+ note: attributes[:note],
47
+ confirmationCode: attributes[:confirmation_code]
48
+ }.compact
49
+ end
50
+
51
+ # @rbs (::Time | ::Date value) -> ::String
52
+ def at(value)
53
+ return value.utc.iso8601(3) if value.is_a?(Time)
54
+
55
+ Time.utc(value.year, value.month, value.day).iso8601(3)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,21 +1,8 @@
1
1
  module Guesty
2
- # Faraday request middleware delegating throttling to the injected limiter
3
- # (Guesty.config.rate_limiter), so a host can share the windows across
4
- # processes. Registered as :guesty_rate_limiter.
5
2
  class RateLimiterMiddleware < Faraday::Middleware
6
- # @rbs (untyped app, ?::Hash[::Symbol, untyped] options) -> void
7
- def initialize(app, options = {})
8
- super
9
-
10
- @key = options[:key]
11
- @threshold = options[:threshold]
12
- @interval = options[:interval]
13
- end
14
-
3
+ # @rbs (untyped env) -> void
15
4
  def on_request(_env)
16
- key = @key.is_a?(Proc) ? @key.call : @key
17
-
18
- Guesty.config.rate_limiter.throttle!(key:, threshold: @threshold, interval: @interval)
5
+ Guesty.config.rate_limiter.throttle!(key: options[:key].call, threshold: options[:threshold], interval: options[:interval])
19
6
  end
20
7
  end
21
8
 
@@ -15,20 +15,19 @@ module Guesty
15
15
  adjustment = amount.to_d - current_fare(reservation_id)
16
16
  return Success(adjustment) if adjustment.zero?
17
17
 
18
- response = Guesty.do_request :post, 'price-adjustments/manual-total-amount', payload(reservation_id, adjustment), retries: 0
19
- return response unless response.success?
18
+ request(:post, 'price-adjustments/manual-total-amount', payload(reservation_id, adjustment), retries: 0)
20
19
 
21
20
  Success(adjustment)
22
21
  end
23
22
 
24
23
  private
25
24
 
25
+ # An unknown reservation has no folio items; price-adjustments then rejects the write.
26
26
  # @rbs (::String reservation_id) -> ::BigDecimal
27
27
  def current_fare(reservation_id)
28
- reservation = Show.new.call!(reservation_id:, fields: %w[money.invoiceItems]).value!
29
- items = reservation.dig(:money, :invoiceItems)
28
+ items = GuestFolio::InvoiceItems::Index.new.call!(reservation_id:).value!
30
29
 
31
- items.select { |item| FARE_TYPES.include?(item[:normalType]) }.sum(BigDecimal('0')) { |item| item[:amount].to_d }
30
+ items.select { |item| FARE_TYPES.include?(item[:normal_type]) }.sum(0.to_d) { |item| item[:amount].to_d }
32
31
  end
33
32
 
34
33
  # @rbs (::String reservation_id, ::BigDecimal adjustment) -> ::Hash[::Symbol, untyped]
@@ -1,7 +1,6 @@
1
1
  module Guesty
2
2
  module Reservations
3
- # Approves a request to book (a reservation in status `reserved`). Runs
4
- # asynchronously on Guesty's side; a repeat answers 400 DUPLICATE.
3
+ # Asynchronous on Guesty's side; a repeat answers 400 DUPLICATE.
5
4
  class Approve
6
5
  include Guesty::Operation
7
6
 
@@ -9,12 +8,7 @@ module Guesty
9
8
  def call(reservation_id:)
10
9
  argument! reservation_id: :required if reservation_id.blank?
11
10
 
12
- response = Guesty.do_request(
13
- :post,
14
- "reservations-v3/#{reservation_id}/approve",
15
- retries: 0
16
- )
17
- return response unless response.success?
11
+ request(:post, "reservations-v3/#{reservation_id}/approve", retries: 0)
18
12
 
19
13
  Success(true)
20
14
  end
@@ -0,0 +1,19 @@
1
+ module Guesty
2
+ module Reservations
3
+ module BookingDate
4
+ class Update
5
+ include Guesty::Operation
6
+
7
+ # @rbs (reservation_id: ::String, booking_date: ::Time) -> Dry::Monads::Result[bool]
8
+ def call(reservation_id:, booking_date:)
9
+ argument! reservation_id: :required if reservation_id.blank?
10
+ argument! booking_date: :required if booking_date.blank?
11
+
12
+ request(:put, "reservations-v3/#{reservation_id}/booking-date", { bookingDate: booking_date.utc.iso8601(3) })
13
+
14
+ Success(true)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module Guesty
2
+ module Reservations
3
+ module ConfirmationCode
4
+ class Update
5
+ include Guesty::Operation
6
+
7
+ LENGTH = 5..30 #: ::Range
8
+
9
+ # @rbs (reservation_id: ::String, confirmation_code: ::String) -> Dry::Monads::Result[bool]
10
+ def call(reservation_id:, confirmation_code:)
11
+ argument! reservation_id: :required if reservation_id.blank?
12
+ argument! confirmation_code: :required if confirmation_code.blank?
13
+ argument! confirmation_code: :invalid unless LENGTH.cover?(confirmation_code.length)
14
+
15
+ request(:put, "reservations-v3/#{reservation_id}/confirmation-code", { confirmationCode: confirmation_code })
16
+
17
+ Success(true)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -19,8 +19,7 @@ module Guesty
19
19
  def call(attributes:)
20
20
  validate_attributes!(attributes)
21
21
 
22
- response = Guesty.do_request :post, 'reservations-v3', attributes, retries: 0, timeout: 60
23
- Success(handle_response(response))
22
+ Success(request(:post, 'reservations-v3', attributes, retries: 0, timeout: 60).fetch(:reservationId))
24
23
  end
25
24
 
26
25
  private
@@ -39,12 +38,6 @@ module Guesty
39
38
  argument! key => :required if guests[key].blank?
40
39
  end
41
40
  end
42
-
43
- def handle_response(response)
44
- raise response.failure unless response.success?
45
-
46
- response.value!.body.fetch(:reservationId)
47
- end
48
41
  end
49
42
  end
50
43
  end
@@ -4,21 +4,12 @@ module Guesty
4
4
  class Update
5
5
  include Guesty::Operation
6
6
 
7
- # @rbs (reservation_id: ::String, custom_fields: ::Array[::Hash[::Symbol, untyped]]) -> Dry::Monads::Result[::Hash[::Symbol, untyped]]
7
+ # @rbs (reservation_id: ::String, custom_fields: ::Array[::Hash[::Symbol, untyped]]) -> Dry::Monads::Result[untyped]
8
8
  def call(reservation_id:, custom_fields:)
9
9
  argument! reservation_id: :required if reservation_id.blank?
10
10
  argument! custom_fields: :required if custom_fields.blank?
11
11
 
12
- response = Guesty.do_request :put, "reservations-v3/#{reservation_id}/custom-fields", { customFields: custom_fields }
13
- Success(handle_response(response))
14
- end
15
-
16
- private
17
-
18
- def handle_response(response)
19
- raise response.failure unless response.success?
20
-
21
- response.value!.body
12
+ Success(request(:put, "reservations-v3/#{reservation_id}/custom-fields", { customFields: custom_fields }))
22
13
  end
23
14
  end
24
15
  end
@@ -9,14 +9,7 @@ module Guesty
9
9
  argument! reservation_id: :required if reservation_id.blank?
10
10
  argument! attributes: :required if attributes.blank?
11
11
 
12
- payload = date_attributes(attributes)
13
- response = Guesty.do_request(
14
- :put,
15
- "reservations-v3/#{reservation_id}/dates",
16
- payload
17
- )
18
-
19
- raise response.failure if response.failure?
12
+ request(:put, "reservations-v3/#{reservation_id}/dates", date_attributes(attributes))
20
13
 
21
14
  Success(true)
22
15
  end
@@ -32,6 +25,7 @@ module Guesty
32
25
  plannedDeparture: attributes[:check_out]&.strftime('%H:%M'),
33
26
  ignoreCalendar: attributes[:ignore_calendar],
34
27
  ignoreTerms: attributes[:ignore_terms],
28
+ ignoreBlocks: attributes[:ignore_blocks],
35
29
  applyRecalculation: false
36
30
  }.compact
37
31
  end
@@ -11,17 +11,7 @@ module Guesty
11
11
  argument! reason: :required if reason.blank?
12
12
  argument! message: :required if message.blank?
13
13
 
14
- body = {
15
- reason:,
16
- messageToGuest: message
17
- }
18
- response = Guesty.do_request(
19
- :post,
20
- "reservations-v3/#{reservation_id}/decline",
21
- body,
22
- retries: 0
23
- )
24
- return response unless response.success?
14
+ request(:post, "reservations-v3/#{reservation_id}/decline", { reason:, messageToGuest: message }, retries: 0)
25
15
 
26
16
  Success(true)
27
17
  end
@@ -0,0 +1,43 @@
1
+ module Guesty
2
+ module Reservations
3
+ module Guests
4
+ class Update
5
+ include Guesty::Operation
6
+
7
+ # @rbs (reservation_id: ::String, attributes: ::Hash[::Symbol, untyped]) -> Dry::Monads::Result[bool]
8
+ def call(reservation_id:, attributes:)
9
+ argument! reservation_id: :required if reservation_id.blank?
10
+ argument! attributes: :required if attributes.blank?
11
+ argument! num_guests: :required if attributes[:num_guests].blank?
12
+
13
+ request(:put, "reservations-v3/#{reservation_id}/guests", payload(attributes))
14
+
15
+ Success(true)
16
+ end
17
+
18
+ private
19
+
20
+ # @rbs (::Hash[::Symbol, untyped] attributes) -> ::Hash[::Symbol, untyped]
21
+ def payload(attributes)
22
+ {
23
+ guestsCount: attributes[:num_guests],
24
+ numberOfGuests: number_of_guests(attributes),
25
+ ignoreCalendar: attributes[:ignore_calendar],
26
+ ignoreTerms: attributes[:ignore_terms],
27
+ ignoreBlocks: attributes[:ignore_blocks]
28
+ }.compact
29
+ end
30
+
31
+ # @rbs (::Hash[::Symbol, untyped] attributes) -> ::Hash[::Symbol, untyped]?
32
+ def number_of_guests(attributes)
33
+ {
34
+ numberOfAdults: attributes[:adults],
35
+ numberOfChildren: attributes[:children],
36
+ numberOfInfants: attributes[:infants],
37
+ numberOfPets: attributes[:pets]
38
+ }.compact.presence
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,18 @@
1
+ module Guesty
2
+ module Reservations
3
+ # Unknown ids are silently absent from the response.
4
+ class Index
5
+ include Guesty::Operation
6
+
7
+ TAKE = 10 #: ::Integer # Max allowed by the Guesty API
8
+
9
+ # @rbs (reservation_ids: ::Array[::String]) -> Dry::Monads::Result[::Array[::Hash[::Symbol, untyped]]]
10
+ def call(reservation_ids:)
11
+ argument! reservation_ids: :required if reservation_ids.blank?
12
+ argument! reservation_ids: :invalid if reservation_ids.size > TAKE
13
+
14
+ Success(request(:get, 'reservations-v3', { reservationIds: reservation_ids }))
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,7 +1,6 @@
1
1
  module Guesty
2
2
  module Reservations
3
- # Pre-approves an inquiry (Airbnb only, asynchronous). An inquiry cannot be
4
- # declined through the API.
3
+ # Airbnb only. An inquiry cannot be declined through the API.
5
4
  class PreApprove
6
5
  include Guesty::Operation
7
6
 
@@ -9,12 +8,7 @@ module Guesty
9
8
  def call(reservation_id:)
10
9
  argument! reservation_id: :required if reservation_id.blank?
11
10
 
12
- response = Guesty.do_request(
13
- :post,
14
- "reservations-v3/#{reservation_id}/pre-approve",
15
- retries: 0
16
- )
17
- return response unless response.success?
11
+ request(:post, "reservations-v3/#{reservation_id}/pre-approve", retries: 0)
18
12
 
19
13
  Success(true)
20
14
  end
@@ -1,26 +1,18 @@
1
1
  module Guesty
2
2
  module Reservations
3
- # Finds the Guesty reservation matching a confirmation code and stay window,
4
- # skipping canceled ones. Returns nil when none matches.
5
3
  class Recover
6
4
  include Guesty::Operation
7
5
 
8
6
  # @rbs (confirmation_code: ::String, start_date: ::Date, end_date: ::Date) -> Dry::Monads::Result[::String?]
9
7
  def call(confirmation_code:, start_date:, end_date:)
10
- fields = %w[_id listingId checkInDateLocalized checkOutDateLocalized status confirmationCode]
11
- attributes = { confirmation_code: }
12
- result = Guesty::Reservations::Search.new.call!(
13
- fields:,
14
- attributes:,
15
- take: 2
16
- ).value!
17
- candidate = result.fetch(:results).find do |reservation|
8
+ reservations = Search.new.call!(attributes: { confirmation_code: }, take: 2).value!.fetch(:results)
9
+ match = reservations.find do |reservation|
18
10
  reservation[:status] != CANCELLED_STATUS &&
19
11
  reservation[:checkInDateLocalized] == start_date.iso8601 &&
20
12
  reservation[:checkOutDateLocalized] == end_date.iso8601
21
13
  end
22
14
 
23
- Success(candidate&.dig(:_id))
15
+ Success(match&.dig(:reservationId))
24
16
  end
25
17
  end
26
18
  end