-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(general): update expenses collection (#698)
- Loading branch information
Showing
15 changed files
with
133 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { buildProperties } from 'firecms'; | ||
import { Expense, EXPENSES_FIRESTORE_PATH, ExpenseType } from '../../../shared/src/types/expense'; | ||
import { buildAuditedCollection } from './shared'; | ||
|
||
export const expensesCollection = buildAuditedCollection<Expense>({ | ||
name: 'Expenses', | ||
group: 'Finances', | ||
path: EXPENSES_FIRESTORE_PATH, | ||
textSearchEnabled: false, | ||
icon: 'LocalAtm', | ||
description: 'Project expenses displayed on transparency page', | ||
initialSort: ['year', 'desc'], | ||
permissions: { | ||
edit: true, | ||
create: true, | ||
delete: true, | ||
}, | ||
properties: buildProperties<Expense>({ | ||
type: { | ||
dataType: 'string', | ||
name: 'Type', | ||
enumValues: [ | ||
{ id: ExpenseType.DeliveryFees, label: 'Delivery fees' }, | ||
{ id: ExpenseType.DonationFees, label: 'Donation fees' }, | ||
{ id: ExpenseType.ExchangeRateFluctuation, label: 'Exchange rate fluctuation' }, | ||
], | ||
validation: { required: true }, | ||
}, | ||
year: { | ||
dataType: 'number', | ||
name: 'Year', | ||
enumValues: { 2020: '2020', 2021: '2021', 2022: '2022', 2023: '2023', 2024: '2024', 2025: '2025' }, | ||
validation: { required: true }, | ||
}, | ||
amount_chf: { | ||
dataType: 'number', | ||
name: 'Amount Chf', | ||
validation: { required: true }, | ||
}, | ||
}), | ||
}); |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
{ | ||
"version": "12.9.1", | ||
"version": "13.0.2", | ||
"firestore": { | ||
"version": "1.18.2", | ||
"path": "firestore_export", | ||
"metadata_file": "firestore_export/firestore_export.overall_export_metadata" | ||
}, | ||
"auth": { | ||
"version": "12.9.1", | ||
"version": "13.0.2", | ||
"path": "auth_export" | ||
}, | ||
"storage": { | ||
"version": "12.9.1", | ||
"version": "13.0.2", | ||
"path": "storage_export" | ||
} | ||
} |
Binary file modified
BIN
+0 Bytes
(100%)
seed/firestore_export/all_namespaces/all_kinds/all_namespaces_all_kinds.export_metadata
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
seed/firestore_export/firestore_export.overall_export_metadata
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const EXPENSES_FIRESTORE_PATH = 'expenses'; | ||
|
||
export enum ExpenseType { | ||
DonationFees = 'donation_fees', | ||
DeliveryFees = 'delivery_fees', | ||
ExchangeRateFluctuation = 'exchange_rate_fluctuation', | ||
} | ||
|
||
export type Expense = { | ||
type: ExpenseType; | ||
year: number; | ||
amount_chf: number; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import _ from 'lodash'; | ||
import { FirestoreAdmin } from '../../firebase/admin/FirestoreAdmin'; | ||
import { Currency } from '../../types/currency'; | ||
import { Expense, EXPENSES_FIRESTORE_PATH, ExpenseType } from '../../types/expense'; | ||
import { getLatestExchangeRate } from '../exchangeRates'; | ||
|
||
type ExpenseStatsEntry = Expense & { | ||
amount: number; | ||
}; | ||
|
||
export class ExpensesStatsCalculator { | ||
private expenses: _.Collection<ExpenseStatsEntry>; | ||
|
||
private constructor(expenses: _.Collection<ExpenseStatsEntry>) { | ||
this.expenses = expenses; | ||
} | ||
|
||
public static async build(firestoreAdmin: FirestoreAdmin, currency: Currency): Promise<ExpensesStatsCalculator> { | ||
let expenses = await firestoreAdmin.collection<Expense>(EXPENSES_FIRESTORE_PATH).get(); | ||
const exchangeRate = await getLatestExchangeRate(firestoreAdmin, currency); | ||
|
||
return new ExpensesStatsCalculator( | ||
_(expenses.docs).map((doc) => { | ||
const data = doc.data(); | ||
return { amount: exchangeRate * data.amount_chf, ...data }; | ||
}), | ||
); | ||
} | ||
|
||
public totalExpensesBy(group: 'type' | 'year'): { [type in string]?: number } { | ||
return this.expenses | ||
.groupBy(group) | ||
.map((expenses, group) => ({ [group]: _.sumBy(expenses, 'amount') })) | ||
.reduce((a, b) => ({ ...a, ...b }), {}); | ||
} | ||
|
||
public totalExpensesByYearAndType(): { [year: string]: { [type in ExpenseType]: number } } { | ||
return this.expenses | ||
.groupBy('year') | ||
.map((expenses, year) => ({ | ||
[year]: _(expenses) | ||
.groupBy('type') | ||
.map((objs, type) => ({ [type]: _.sumBy(objs, 'amount') })) | ||
.reduce((a, b) => ({ ...a, ...b }), {}), | ||
})) | ||
.reduce((a, b) => ({ ...a, ...b }), {}); | ||
} | ||
|
||
public allStats() { | ||
return { | ||
totalExpensesByYear: this.totalExpensesBy('year'), | ||
totalExpensesByType: this.totalExpensesBy('type'), | ||
totalExpensesByYearAndType: this.totalExpensesByYearAndType(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters