Database Reference
In-Depth Information
update
=
{
'$set'
: {
'status'
:
'pending'
,
'last_modified'
:
now
} } )
iif
not
not
result
[
'updatedExisting'
]:
raise
raise
CartInactive
()
try
try
:
collect_payment
(
cart
)
except
except
:
db
.
cart
.
update
(
{
'_id'
:
cart_id
},
{
'$set'
: {
'status'
:
'active'
} } )
raise
raise
db
.
cart
.
update
(
{
'_id'
:
cart_id
},
{
'$set'
: {
'status'
:
'complete'
} } )
db
.
product
.
update
(
{
'carted.cart_id'
:
cart_id
},
{
'$pull'
: {
'cart_id'
:
cart_id
} },
multi
=
True
)
We're using the return value from
update
here to let us know whether we actually
locked
a currently
active
cart by moving it to
pending
status.
We're using
multi=True
here to ensure that
all
the SKUs that were in the cart have
their
carted
properties updated.
Here, we could actually use a new index on the
carted.cart_id
property so that our final
update
is fast:
>>>
>>>
db
.
product
.
ensure_index
(
'carted.cart_id'
)
Returning inventory from timed-out carts
Periodically, we need to “expire” inactive carts and return their items to available inventory.
Our approach here is as follows:
1. Find all carts that are older than the
threshold
and are due for expiration. Lock them
by setting their status to
"expiring"
.
2. For each
"expiring"
cart, return all their items to available inventory.
3. Once the
product
collection has been updated, set the cart's status to
"expired"
.