kvs_purchase.erl 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -module(kvs_purchase).
  2. -copyright('Synrc Research Center s.r.o.').
  3. -include_lib("kvs/include/products.hrl").
  4. -include_lib("kvs/include/purchases.hrl").
  5. -include_lib("kvs/include/log.hrl").
  6. -include_lib("kvs/include/config.hrl").
  7. -include_lib("kvs/include/accounts.hrl").
  8. -include_lib("kvs/include/feed_state.hrl").
  9. -compile(export_all).
  10. solvent(UId, ProductId) ->
  11. {ok, Credit} = kvs_accounts:balance(UId, currency),
  12. {ok, #product{price = Price}} = kvs:get(product, ProductId),
  13. Credit >= Price.
  14. buy(UId, ProductId) ->
  15. {ok, #product{price = Price, name = Name}} = kvs:get(product, ProductId),
  16. kvs_accounts:transaction(UId, currency, -Price, "Buy " ++ Name ++ " for "++ integer_to_list(Price)),
  17. kvs:put(#user_product{username=UId, timestamp=now(), product_id = ProductId}).
  18. give(UId, ProductId) ->
  19. kvs:put(#user_product{username=UId, timestamp=now(), product_id = ProductId}).
  20. products(UId) -> DBA=?DBA, DBA:products(UId).
  21. handle_notice(["kvs_purchase", "user", UId, "buy"] = Route,
  22. Message, #state{owner = Owner, type =Type} = State) ->
  23. ?INFO(" queue_action(~p): buy_gift: Owner=~p, Route=~p, Message=~p", [self(), {Type, Owner}, Route, Message]),
  24. {GId} = Message,
  25. buy(UId, GId),
  26. {noreply, State};
  27. handle_notice(["kvs_purchase", "user", UId, "give"] = Route,
  28. Message, #state{owner = Owner, type =Type} = State) ->
  29. ?INFO(" queue_action(~p): give_gift: Owner=~p, Route=~p, Message=~p", [self(), {Type, Owner}, Route, Message]),
  30. {GId} = Message,
  31. give(UId, GId),
  32. {noreply, State};
  33. handle_notice(Route, Message, State) -> error_logger:info_msg("Unknown PURCHASE notice").