Skip to content

Commit

Permalink
feat: #6210 #6325 admin usage download, validations fix (#6436)
Browse files Browse the repository at this point in the history
* feat: #6210 #6325 admin usage download, validations fix

* fix: coverage
  • Loading branch information
willmcvay authored Mar 22, 2022
1 parent fe45781 commit 73a4cd2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/admin-portal/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
global: {
branches: 63,
functions: 70,
lines: 85,
lines: 80,
statements: 83,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Yup from 'yup'
import errorMessages from '@/constants/error-messages'
import { FormFieldInfo, reapitRefRegex } from '@reapit/utils-common'
import { FormFieldInfo } from '@reapit/utils-common'

export type FieldKey = 'status' | 'reapitReference'
export const formFields: Record<FieldKey, FormFieldInfo> = {
Expand All @@ -24,6 +24,6 @@ export const validationSchema = Yup.object().shape({
.trim()
.when(status.name, {
is: (val) => val == 'confirmed',
then: Yup.string().required(errorMessages.FIELD_REQUIRED).matches(reapitRefRegex, reapitReference.errorMessage),
then: Yup.string().required(errorMessages.FIELD_REQUIRED).max(50, 'Maximum of 50 characters allowed'),
}),
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { StatisticsRequestParams, fetchStatistics } from '@/actions/statistics'
import { getRangeName } from '@/utils/statistics'
import { selectStatistics } from '@/selector/admin'
import Papa from 'papaparse'
import { AppSummaryModel, DeveloperModel, InstallationModel } from '@reapit/foundations-ts-definitions'
import {
AppSummaryModel,
DeveloperModel,
InstallationModel,
ServiceItemBillingV2Model,
} from '@reapit/foundations-ts-definitions'
import FileSaver from 'file-saver'

export type Area = 'APPS' | 'DEVELOPERS' | 'INSTALLATIONS'
export type Area = 'APPS' | 'DEVELOPERS' | 'INSTALLATIONS' | 'BILLING'
export type Range = 'WEEK' | 'MONTH' | 'ALL'

export interface InstallationModelWithAppName extends InstallationModel {
Expand All @@ -25,7 +30,10 @@ export const handleSaveFile = (csv: string, filename: string) => {
FileSaver.saveAs(blob, filename)
}

const downloadCSV = (area: Area, data: AppSummaryModel[] | DeveloperModel[] | InstallationModelWithAppName[]) => {
export const downloadCSV = (
area: Area,
data: AppSummaryModel[] | DeveloperModel[] | InstallationModelWithAppName[] | ServiceItemBillingV2Model[],
) => {
if (area === 'APPS') {
const apps = data as AppSummaryModel[]
const csv = Papa.unparse({
Expand Down Expand Up @@ -74,6 +82,21 @@ const downloadCSV = (area: Area, data: AppSummaryModel[] | DeveloperModel[] | In

return handleSaveFile(csv, 'developers.csv')
}

if (area === 'BILLING') {
const apiCalls = data as ServiceItemBillingV2Model[]

const csv = Papa.unparse({
fields: ['Entity Name', 'Total Number Calls', 'Total Cost'],
data: apiCalls.map((item: ServiceItemBillingV2Model) => {
const { name, amount, cost } = item
const formattedCost = cost ? ${cost.toFixed(2).padStart(2, '0')}` : '£0'
return [name, amount, formattedCost]
}),
})

return handleSaveFile(csv, 'billing.csv')
}
}

export const Statistics: React.FC = () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/admin-portal/src/components/pages/usage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loader, Title } from '@reapit/elements'
import { Button, ButtonGroup, Loader, Title } from '@reapit/elements'
import React, { FC, useState } from 'react'
import { FilterForm } from './filter-form'
import { UsageTable } from './usage-table'
Expand All @@ -10,6 +10,7 @@ import {
BillingBreakdownForMonthV2Model,
InstallationModelPagedResult,
} from '@reapit/foundations-ts-definitions'
import { downloadCSV } from '../statistics/statistics'

export interface UsageFilters {
month?: string
Expand All @@ -18,6 +19,11 @@ export interface UsageFilters {
appId?: string
}

export const handleCsvDownload = (billing: BillingBreakdownForMonthV2Model | null) => () => {
const apiCalls = billing?.services?.find((item) => item.name === 'API Requests')?.items ?? []
downloadCSV('BILLING', apiCalls)
}

export const UsagePage: FC = () => {
const [usageFilters, setUsageFilters] = useState<UsageFilters>({})
const { month, developerId, appId, customerId } = usageFilters
Expand Down Expand Up @@ -66,6 +72,13 @@ export const UsagePage: FC = () => {
<>
<Title>API Usage</Title>
<FilterForm setUsageFilters={setUsageFilters} apps={apps} installations={installations} />
{billing && (
<ButtonGroup alignment="right">
<Button intent="primary" onClick={handleCsvDownload(billing)}>
Download Results
</Button>
</ButtonGroup>
)}
{billingLoading ? <Loader /> : <UsageTable billing={billing} />}
</>
)
Expand Down
2 changes: 2 additions & 0 deletions packages/admin-portal/src/utils/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const getDataLabel = (area: Area): string => {
return 'Developers'
case 'INSTALLATIONS':
return 'Installations'
case 'BILLING':
return 'Billing'
}
}

Expand Down

0 comments on commit 73a4cd2

Please sign in to comment.