What is the best possible user multi-currency wallet database design? - mongodb

I'm using mongodb/mongoose, my app new requirements is to have multiple currencies, and each user can have a different balance for each currency.
ex: A user can have 30 USD, 100 EUR, 200 EGP.
No exchange from one currency to another, just a bunch of currencies.
At the moment we don't have any currencies, just a field called "balance" which contains user's balance.
My solution is to create a table for currencies, each with an _id and a name.
but when it comes to user balance I can't determine what is the best solution to make increasing and decreasing user balance easy, the only solution that crosses my mind is making balance field an array of object
balance: [{ currency: "CurrenyId", amount: 50 }, { currency: "CurrenyId", amount: 100 }]
But the problem with this approach is updating balance would be troublesome, Would you suggest a better solution ?
thanks

Related

Several questions about Core ML and Create ML in Swift

I have a pretty simple .csv table with data about flat prices in several cities around the world for the years by date (city ID, city name, date, rooms, price and discount).
I want to predict flat price AND discount on the given date in the future.
The price is indicated without the discount, therefore we will assume that it hasn’t any connection with the discount and a discount CANNOT be presented in the form of (price - discount) formula.
Discount can be only 5, 15 or 30%.
What is the best way to set a date in table? (Can I represent date
as Double in DDMMYYYY format?)
What is the best way to set a discount in table? (And in which
format should I get it in the output?)
Can I get two outputs in one data model or should I split them into
two?
Which algorithms should I choose (MLRegressor, MLClassifier or something specific)?
Thank you!

Calculate a running total that works with relative date filters

I have a Union table of my various bank accounts to create a personal finance analysis dashboard.
I am trying to make a Running Total to show my total capital available at any given date. Using a Running Total table calculation works, just as much as using a RUNNING_SUM() calculated field. They both work up until I filter the dates. So I am trying to find a way to make the running calculation work without being thrown off by Date Filters (I would like to implement relative dates for visualisation in the dashboard).
My union table has the following relevant data columns:
Order ID: Descending number from 1 for each entry per account.
Date: Date of entry.
Item: Entry name.
Account: Name of bank account.
Amount: +ive for credit or -ive for debit.
Balance: balance after entry value for each given account.
So the table can look like this:
So on 07/05/2019 the Running total should be 229.64.
The running sum formula mentioned above is currently RUNNING_SUM(SUM([Amount])), so if any dates are excluded via filter the running total doesn't add up to the right amount.
A way I can see around the problem could be to get the sum over all accounts of the last balance reading at a given date. The balance is a running total but only if the final entry per time period for all accounts are summed would it work. Would it be possible to make a calculated field that gets the last balance reading for each account at any given date and then sums them?
Or is there a simpler smarter way I am not aware of?
This comes down to an Order of Operations problem. Once you filter the dates the viz doesn't have access to the data anymore.
Your best approach would be to add the running sum to the data source before you bring it into Tableau. Then the running sum isn't a calculated field dependent on the data in the Viz.

Rails 4 + PostgreSQL (Heroku): complex/scalable queries for analysis/reporting on data

Working on a financial application that tracks sales. However, I'm running into problems trying to create a schema for properly tracking the data for reports (the main point of the app).
A purchase is the foundation of the app. It has several associations (listed below). Each purchase is tracked via a year and month field. A year is the smallest unit a user may filter a report by, so I will only have to show data for each month in that year.
# purchase.rb model
class Purchase < ActiveRecord::Base
# Associations:
# belongs_to :partner
# belongs_to :purchase_type
# belongs_to :purchase_category
# Attributes:
# partner_id => association
# purchase_type_id => association
# purchase_category_id => association
# year => year in integer (2013, 2014, etc...)
# month => month in integer ("January" => 1, etc...)
# amount => amount a product sold for in cents ($10.00 => 1000)
# fee => fee for associated partner (if there is one) in cents ($2.00 => 200)
end
The problem is that I need to show an overview for a given year, which breaks things down by how many purchases were completed, what partners completed them, and what were the fee amounts. I solved that by having YearMetric and MonthMetric tables that are updated everytime a purchase is added/updated/removed. So you add a new purchase for a given year/month, and the corresponding YearMetric and MonthMetric rows are found and updated with +/- the appropriate amounts/fees.
This solution works well for the overview page. However, I also need to be able to view purchases in the context of partners, purchase_types, and purchase_categories. If I followed the same strategy as my overview report, I would have to add the following tables:
PartnerYearMetric, PartnerMonthMetric
PurchaseCategoryYearMetric, PurchaseCategoryMonthMetric
PurchaseTypeYearMetric, PurchaseTypeMonthMetric
So everytime I add a purchase, I would be doing up to 8 additional DB updates (8 finds and then 8 updates).
The items I'm reporting on are total purchases made, average purchases (historical comparison), total amounts/fees for the period, top partners by number of purchases and by most fee amounts, etc...
There has to be a better solution than this. "Live calculation" by updating 8 records for every 1 purchase seems a bit overkill.
What you're doing is maintaining materialized views of the data in the application. It's a form of denormalization. That can be OK as an optimization but should not be your first choice. It can be very error prone, especially in the presence of concurrency, and must be done quite carefully.
Instead, when you wish to generate a summary report, use an aggregate to SUM them, COUNT them, etc as appropriate. See aggregate functions in the Pg docs, rails Calculations, rails aggregates.
You may find it convenient to create a VIEW over the query you use, and then access the view from the application.
If you find performance of calculating the aggregates in real time for the summary to be a problem, and you cannot solve it with proper indexing and tuning, then you should think about denormalizing. Rather than maintaining your materialized views in the app, though, consider using triggers in the database; they're much easier to write in a concurrency-safe way.
You may also want to look up PostgreSQL 9.4's enhanced materialized views support.

How to add dynamic discount amount for one coupon in Magento

How to set dynamic discount amount if an coupon is applied.
For this i have created a shopping cart price rule and send this promo coupon to some customers and when they purchase products from my site and applied this coupon every customers get different discount amount based on the products in their shopping cart so how i can set this dynamic discount amount ?
I have already make some research on salesrule/rule module but getting confused so please help me...
Just for some your knowledge or someone needy finally i have found solution for my question for dynamic discount amount.
to achieve this you need to change in two methods first one is process(Mage_Sales_Model_Quote_Item_Abstract $item) under Mage_SalesRule_Model_Validator class in which you need to change the rule discount amount and set it to any dynamic calculated value like
$rule->setDiscountAmount($dynamicDiscountAmount);
and in the collect method just update the discount amount for display purpose in totals block
$address->setDiscountAmount(-$discountAmount);
$address->setBaseDiscountAmount(-$discountAmount);

Paypal IPN using PHP to get customized Quantity and Amount

I have downloaded the sample paypal IPN script using php and customised with the email for the buyout option and it has also some hidden paramaters such as amount, quantity,return_url, notify_url like this. My main idea is to do the buyout option for the product purchase in my website.The current calculation for the product purchase is as follows:
Item Price:$20
Quantity :10
Total amount $200
I need the calculation to some different way as our site have some static set of quantities and prices, so our point of calculation is as follows:
Item Price:$20
Quantity :10
Total amount:$20
So, the total amount needs to be the same for the given number of quantities.The IPN configuration doesn't allow me to change the quantity field by this way, please suggest some ideas to overcome this issue.
The item price is price per item, the quantity is of course multiplied by the item price. Your second example should have $2 as the --Item-- price. If you want to sell "10 items for $20", I think you will need to make a new item for that combo and just but the actual quantity in a custom field.
If you want to use the same item and paypal's quantity field, I think you need to look into calculating a discount and making that part of the parameters (discount_amount_cart or discount_amount_X for item X I think).