Skip to content

Commit bb06436

Browse files
Merge pull request #540 from biocore/csymons_perks_quantity
Fix Perk Fulfillment for Orders with Quantity > 1
2 parents eea1212 + b9f8e10 commit bb06436

File tree

3 files changed

+296
-77
lines changed

3 files changed

+296
-77
lines changed
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- We have three contributions that contained orders for multiple kits but only one was fulfilled.
2+
-- To fulfills the remaining kits, we're going to create duplicate transactions in an unfulfilled state.
3+
4+
-- First, we'll create duplicate transactions with a suffix on the ID to reflect the manual nature of the process.
5+
-- We're going to set the amount and net_amount columns to 0 so that we're not reflecting duplicate revenue.
6+
INSERT INTO campaign.transaction (
7+
id,
8+
interested_user_id,
9+
transaction_type,
10+
remote_campaign_id,
11+
created,
12+
amount,
13+
net_amount,
14+
currency,
15+
payer_first_name,
16+
payer_last_name,
17+
payer_email,
18+
account_type,
19+
message,
20+
subscribed_to_updates
21+
)
22+
SELECT
23+
CONCAT(id, '-manual-patch-132'),
24+
interested_user_id,
25+
transaction_type,
26+
remote_campaign_id,
27+
created,
28+
0,
29+
0,
30+
currency,
31+
payer_first_name,
32+
payer_last_name,
33+
payer_email,
34+
account_type,
35+
message,
36+
subscribed_to_updates
37+
FROM campaign.transaction WHERE id IN ('73A565339S129053J', '9G656551639672613', '8X951176NJ262430Y');
38+
39+
-- Next, we'll create the new records in campaign.fundrazr_transaction_perk.
40+
-- We'll decrement quantity by 1 to reflect the kit that was already processed for each order.
41+
INSERT INTO campaign.fundrazr_transaction_perk (transaction_id, perk_id, quantity, processed)
42+
SELECT CONCAT(transaction_id, '-manual-patch-132'), perk_id, quantity-1, FALSE
43+
FROM campaign.fundrazr_transaction_perk WHERE transaction_id IN ('73A565339S129053J', '9G656551639672613', '8X951176NJ262430Y');
44+
45+
-- Lastly, we're going to set the original records in campaign.fundrazr_transaction_perk to a quantity of 1 to reflect what was fulfilled
46+
UPDATE campaign.fundrazr_transaction_perk SET quantity = 1 WHERE transaction_id IN ('73A565339S129053J', '9G656551639672613', '8X951176NJ262430Y');
47+

microsetta_private_api/repo/perk_fulfillment_repo.py

+79-75
Original file line numberDiff line numberDiff line change
@@ -97,94 +97,70 @@ def process_pending_fulfillment(self, ftp_id):
9797
)
9898
row = cur.fetchone()
9999
if row is not None:
100-
if self._is_subscription(row):
101-
subscription_id = \
102-
self._create_subscription(row['email'],
103-
row['transaction_id'],
104-
row['ftp_id'])
105-
else:
106-
subscription_id = None
107-
108-
# If there are any FFQs attached to the perk, immediately
109-
# fulfill the first one
110-
if row['ffq_quantity'] > 0:
111-
# If the perk is a kit or subscription, send thank you
112-
# email with kit content. Otherwise, send thank you
113-
# for FFQ only
114-
if row['kit_quantity'] > 0:
115-
template = "thank_you_with_kit"
100+
for perk_qty in range(row['quantity']):
101+
if self._is_subscription(row):
102+
subscription_id = \
103+
self._create_subscription(row['email'],
104+
row['transaction_id'],
105+
row['ftp_id'])
116106
else:
117-
template = "thank_you_no_kit"
118-
119-
error_info = self._fulfill_ffq(
120-
row['ftp_id'],
121-
template,
122-
row['email'],
123-
row['first_name'],
124-
subscription_id
125-
)
126-
if error_info is not None:
127-
error_report.append(
128-
f"Error sending FFQ email for ftp_id "
129-
f"{row['ftp_id']}: {error_info}"
130-
)
107+
subscription_id = None
108+
109+
# If there are any FFQs attached to the perk, immediately
110+
# fulfill the first one
111+
if row['ffq_quantity'] > 0:
112+
# If the perk is a kit or subscription, send thank you
113+
# email with kit content. Otherwise, send thank you
114+
# for FFQ only
115+
if row['kit_quantity'] > 0:
116+
template = "thank_you_with_kit"
117+
else:
118+
template = "thank_you_no_kit"
131119

132-
# Then, if there are more FFQs, schedule/fulfill them as
133-
# appropriate based on fulfillment_spacing_number
134-
for x in range(1, row['ffq_quantity']):
135-
if row['fulfillment_spacing_number'] > 0:
136-
fulfillment_date =\
137-
self._future_fulfillment_date(
138-
row['fulfillment_spacing_number'],
139-
row['fulfillment_spacing_unit'],
140-
x
141-
)
142-
self._schedule_ffq(
143-
subscription_id,
144-
fulfillment_date,
145-
False
146-
)
147-
else:
148120
error_info = self._fulfill_ffq(
149121
row['ftp_id'],
150-
row['kit_quantity'],
122+
template,
151123
row['email'],
152-
row['first_name']
124+
row['first_name'],
125+
subscription_id
153126
)
154127
if error_info is not None:
155128
error_report.append(
156129
f"Error sending FFQ email for ftp_id "
157130
f"{row['ftp_id']}: {error_info}"
158131
)
159132

160-
# If there are any kits attached to the perk, immediately
161-
# fulfill the first one
162-
if row['kit_quantity'] > 0:
163-
status, return_val = self._fulfill_kit(
164-
row,
165-
1,
166-
subscription_id
167-
)
168-
if not status:
169-
# Daklapack order failed, let the error percolate
170-
error_report.append(
171-
f"Error placing Daklapack order for ftp_id "
172-
f"{row['ftp_id']}: {return_val}"
173-
)
174-
175-
for x in range(1, row['kit_quantity']):
176-
if row['fulfillment_spacing_number'] > 0:
177-
fulfillment_date =\
178-
self._future_fulfillment_date(
179-
row['fulfillment_spacing_number'],
180-
row['fulfillment_spacing_unit'],
181-
x
133+
# Then, if there are more FFQs, schedule/fulfill them as
134+
# appropriate based on fulfillment_spacing_number
135+
for x in range(1, row['ffq_quantity']):
136+
if row['fulfillment_spacing_number'] > 0:
137+
fulfillment_date =\
138+
self._future_fulfillment_date(
139+
row['fulfillment_spacing_number'],
140+
row['fulfillment_spacing_unit'],
141+
x
142+
)
143+
self._schedule_ffq(
144+
subscription_id,
145+
fulfillment_date,
146+
fulfilled=False
182147
)
183-
self._schedule_kit(subscription_id,
184-
fulfillment_date,
185-
row['dak_article_code'],
186-
False)
187-
else:
148+
else:
149+
error_info = self._fulfill_ffq(
150+
row['ftp_id'],
151+
row['kit_quantity'],
152+
row['email'],
153+
row['first_name']
154+
)
155+
if error_info is not None:
156+
error_report.append(
157+
f"Error sending FFQ email for ftp_id "
158+
f"{row['ftp_id']}: {error_info}"
159+
)
160+
161+
# If there are any kits attached to the perk, immediately
162+
# fulfill the first one
163+
if row['kit_quantity'] > 0:
188164
status, return_val = self._fulfill_kit(
189165
row,
190166
1,
@@ -197,6 +173,34 @@ def process_pending_fulfillment(self, ftp_id):
197173
f"{row['ftp_id']}: {return_val}"
198174
)
199175

176+
for x in range(1, row['kit_quantity']):
177+
if row['fulfillment_spacing_number'] > 0:
178+
fulfillment_date =\
179+
self._future_fulfillment_date(
180+
row['fulfillment_spacing_number'],
181+
row['fulfillment_spacing_unit'],
182+
x
183+
)
184+
self._schedule_kit(subscription_id,
185+
fulfillment_date,
186+
row['dak_article_code'],
187+
fulfilled=False)
188+
else:
189+
# We hard-code a quantity of 1 here because
190+
# the actual quantity is controlled by
191+
# row['quantity'] and row['kit_quantity'].
192+
status, return_val = self._fulfill_kit(
193+
row,
194+
1,
195+
subscription_id
196+
)
197+
if not status:
198+
# Dak order failed, let the error percolate
199+
error_report.append(
200+
f"Error placing Daklapack order for "
201+
f"ftp_id {row['ftp_id']}: {return_val}"
202+
)
203+
200204
cur.execute(
201205
"UPDATE campaign.fundrazr_transaction_perk "
202206
"SET processed = true "

0 commit comments

Comments
 (0)