Skip to content

Commit

Permalink
implemented currency conversion for the given currencies, just need t…
Browse files Browse the repository at this point in the history
…o then use it to do the transactions and convert the initial amount of money when selecting a currency when you first login
  • Loading branch information
NM711 committed Oct 30, 2023
1 parent dfdcdec commit 96fbe82
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 89 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
},
"type": "module",
"dependencies": {
"@dinero.js/currencies": "^2.0.0-alpha.14",
"@supabase/auth-helpers-sveltekit": "^0.10.3",
"@supabase/supabase-js": "^2.38.2",
"dinero.js": "^2.0.0-alpha.14"
"dinero.js": "^2.0.0-alpha.14",
"fast-iban": "^1.2.0"
}
}
2 changes: 2 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ declare global {
supabase: SupabaseClient<Database>
}

type AvailableCurrencies = "USD" | "EUR" | "CAD" | "AUD" | "GBP" | "NOK" | "JPY" | "IDR" | "DKK" | "SEK" | "MXN"

namespace Resources {
interface ICard {
id: string
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY } from "$env/static/public"
import { createSupabaseServerClient } from "@supabase/auth-helpers-sveltekit"
import type { Handle } from "@sveltejs/kit"

export const handle: Handle = async ({ event, resolve }) => {
event.locals.supabase = createSupabaseServerClient({
supabaseUrl: PUBLIC_SUPABASE_URL,
Expand All @@ -16,7 +17,6 @@ export const handle: Handle = async ({ event, resolve }) => {

event.locals.getSession = async () => await getSessionHelper()


return resolve(event, {
filterSerializedResponseHeaders(name) {
return name === 'content-range'
Expand Down
68 changes: 68 additions & 0 deletions src/lib/server/currencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { dinero, convert } from "dinero.js"
import { USD, MXN, EUR, CAD, AUD, SEK, DKK, NOK, JPY, IDR, GBP } from "@dinero.js/currencies";
import type { Currency, Dinero } from "dinero.js";

const currencyConversionMap: { [currency in AvailableCurrencies]: { [currency in AvailableCurrencies]: number } } = {
"USD": { "MXN": 18, "EUR": 0.85, "CAD": 1.25, "AUD": 1.35, "SEK": 9, "DKK": 6.5, "USD": 1, "NOK": 8, "JPY": 110, "GBP": 0.75, "IDR": 14000 },
"MXN": { "USD": 0.05, "EUR": 0.04, "CAD": 0.06, "AUD": 0.07, "SEK": 0.50, "DKK": 0.77, "NOK": 0.63, "JPY": 5.0, "GBP": 0.04, "IDR": 700, "MXN": 1 },
"CAD": { "USD": 0.80, "EUR": 0.68, "MXN": 16.67, "AUD": 0.90, "SEK": 6.67, "DKK": 4.86, "NOK": 7.50, "JPY": 82.35, "GBP": 0.60, "IDR": 12000, "CAD": 1 },
"EUR": { "USD": 1.18, "CAD": 1.47, "MXN": 25.00, "AUD": 1.61, "SEK": 11.76, "DKK": 8.57, "NOK": 10.00, "JPY": 117.65, "GBP": 0.88, "IDR": 15000, "EUR": 1},
"SEK": { "USD": 0.11, "CAD": 0.15, "MXN": 2.00, "EUR": 9.09, "AUD": 0.19, "DKK": 0.73, "NOK": 0.87, "JPY": 9.09, "GBP": 0.07, "IDR": 1500 , "SEK": 1},
"DKK": { "USD": 0.15, "CAD": 0.21, "MXN": 2.00, "EUR": 11.76, "AUD": 0.25, "SEK": 1.37, "NOK": 1.18, "JPY": 13.33, "GBP": 0.10, "IDR": 2000, "DKK": 1},
"JPY": { "USD": 0.0091, "CAD": 0.0121, "MXN": 0.20, "EUR": 0.0085, "AUD": 0.013, "SEK": 0.11, "DKK": 0.075, "NOK": 0.090, "GBP": 0.0073, "IDR": 150, "JPY": 1 },
"IDR": { "USD": 0.0000714, "CAD": 0.0000833, "MXN": 0.0000143, "EUR": 0.0000667, "AUD": 0.0000714, "SEK": 0.0006667, "DKK": 0.0005, "NOK": 0.0005882, "JPY": 0.0067, "GBP": 0.0000667, "IDR": 1 },
"NOK": { "USD": 0.13, "CAD": 0.20, "MXN": 1.25, "EUR": 10.00, "AUD": 0.21, "SEK": 1.09, "DKK": 0.85, "JPY": 11.11, "GBP": 0.08, "IDR": 1700, "NOK": 1 },
"GBP": {"USD": 1.31, "CAD": 1.73, "MXN": 25.63, "EUR": 1.16, "AUD": 1.84, "SEK": 11.47, "DKK": 8.51, "JPY": 148.20, "GBP": 1, "IDR": 18249.30, "NOK": 13.5 },
"AUD": { "USD": 0.60, "CAD": 0.34, "MXN": 1.19, "EUR": 4.76, "AUD": 1, "SEK": 4.76, "DKK": 4.76, "JPY": 52.86, "GBP": 0.54, "IDR": 8095.24, "NOK": 4.76 }
}



class Currencies {
private currencies: Currency<number>[]
constructor () {
this.currencies = [USD, MXN, EUR, CAD, AUD, SEK, DKK, NOK, JPY, IDR, GBP]
}

setDineroObject (amount: number, currency: AvailableCurrencies) {
for (const x of this.currencies) {
if (x.code === currency) {
return dinero({ amount, currency: x })
}
}
}

private setRate (currency: AvailableCurrencies, amount: number) {
return { [`${currency}`]: { amount, scale: 2 } }
}

conversion (from: Dinero<number>, to: AvailableCurrencies): number {
// had to any
let convertedAmount: any
const currentCurrency = from.toJSON().currency.code
const converted = currencyConversionMap[currentCurrency as AvailableCurrencies]
// O(n^2) solution you guys can improve it if you want

for (const x in converted) {
if (to === x) {
const rate = this.setRate(x, converted[x])
for (const k of this.currencies) {
if (k.code === to) {
convertedAmount = convert(from, k, rate)
}
}
}
}
// when you do the conversion you will get something as
// {
// amount: 90000,
// currency: { code: 'MXN', base: 10, exponent: 2 },
// scale: 4
// }
// it seems like it adds an extra 0 which is supposed to be a decimal for whatever reason but, other than that it is accurate.
// 9k mexican pesos / 500 dollars = 18
return convertedAmount.toJSON().amount as number
}
}

export default Currencies
2 changes: 1 addition & 1 deletion src/lib/server/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export async function prepareDerived ({ getSession, supabase }: App.Locals): Pro
if (!derived) throw error(500, "Something went wrong during a critical process of the card retrieval operation!")

return { crypt, derived, userId }
}
}
8 changes: 6 additions & 2 deletions src/routes/set-currency/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fastIBAN from "fast-iban"
import { fail, redirect, error } from '@sveltejs/kit';
import type { Actions } from "./$types";
import currency_list from "$lib/currency_list.json";


export const actions: Actions = {
default: async ({ request, locals: { supabase, getSession } }) => {

Expand All @@ -12,9 +12,13 @@ export const actions: Actions = {
if (!currency_list.some(c => c.code === currency)) return fail(400, {message: "Invalid currency", success: false, error: "currency"})

if(session !== null){
const ibanCountryCode = currency.split("").slice(0, 2).join("")
const { data, error } = await supabase
.from('account')
.update({ currency: currency })
// note that the sketch iban lib asks for 2 values, bionic doesnt have a bank account so he does not know how this
// works (yes this is my excuse)
// so im just gonna add a uuid for the first param and the first 2 initials for country code ig?
.update({ currency, iban: fastIBAN.generateIBAN(crypto.randomUUID(), ibanCountryCode) })
.eq("user_id", session.user.id)

if(error) return fail(400, {message: "Unable to input currency, please try again", success: false})
Expand Down
Loading

0 comments on commit 96fbe82

Please sign in to comment.