Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visning av kundeansvalrlig og bugfixes #561

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 50 additions & 44 deletions apps/server/routers/customer/customerAggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,62 @@ async function createEmployeeForCustomerList(
}
}

const addBy =
<T extends string>(
key: T
): ((acc: number, cp: { [key in T]: number }) => number) =>
(acc, cp) =>
acc + cp[key]
const unique = <T>(list: T[]): T[] => [...new Set(list)]
const getPeriods = (ec: EmployeeCustomers) =>
ec.reg_periods
.toString()
.split(';')
.map((period) => Number(period))
export function createCustomerCardData(
projects: BilledCustomerHours[],
employeesCustomer: EmployeeCustomers[]
employeesCustomer: EmployeeCustomers[],
accountManagerTable: { customer: string; account_manager: string }[]
): CustomerCardsData[] {
const results = {}

const last_reg_periods = [
...new Set(
projects.map(function (o) {
return o.reg_period
})
),
]
const accountManagerMap = accountManagerTable.reduce(
(acc, row) => ({
...acc,
[row.customer]: row.account_manager,
}),
{}
)
const last_reg_periods = unique(projects.map((o) => o.reg_period))
.sort()
.slice(-3)
.slice(-4)

projects.forEach((elem) => {
const curr_el = results[elem.customer]
if (!curr_el) {
results[elem.customer] = {
customer: elem.customer,
billedLastPeriod: 0,
billedTotal: 0,
}
}
if (elem.reg_period == last_reg_periods[last_reg_periods.length - 1]) {
results[elem.customer]['billedLastPeriod'] =
results[elem.customer]['billedLastPeriod'] + elem.hours
}
results[elem.customer]['billedTotal'] =
results[elem.customer]['billedTotal'] + elem.hours
})
const result = unique(projects.map((ec) => ec.customer)).map((customer) => {
const customerProjects = projects.filter((p) => p.customer === customer)
const customerEmployees = employeesCustomer.filter(
(ec) => ec.customer === customer
)
const employeesLastPeriod = customerEmployees
.filter((ce) => getPeriods(ce).includes(last_reg_periods.at(-1)))
.map((ce) => ce.email)
const employeesLastLongPeriod = customerEmployees
.filter((ce) =>
getPeriods(ce).some((period) => last_reg_periods.includes(period))
)
.map((ce) => ce.email)

Object.keys(results).forEach((customer) => {
const consultants = new Set()
employeesCustomer.forEach((employeeCustomer) => {
if (employeeCustomer.customer == customer) {
const reg_periods =
employeeCustomer?.reg_periods
.toString()
.split(';')
.map((period) => Number(period)) || []
if (last_reg_periods.some((p) => reg_periods.includes(p))) {
consultants.add(employeeCustomer.user_id)
}
}
})
if (consultants.size === 0) delete results[customer]
else results[customer]['consultants'] = consultants.size
return {
customer,
accountManager: accountManagerMap[customer],
billedLastPeriod: customerProjects
.filter((cp) => cp.reg_period === last_reg_periods.at(-1))
.reduce(addBy('hours'), 0),
billedLastLongPeriod: customerProjects
.filter((cp) => last_reg_periods.includes(cp.reg_period))
.reduce(addBy('hours'), 0),
billedTotal: customerProjects.reduce(addBy('hours'), 0),
consultantsLastPeriod: unique(employeesLastPeriod).length,
consultantsLastLongPeriod: unique(employeesLastLongPeriod).length,
}
})

return Object.values(results)
return result
}
9 changes: 8 additions & 1 deletion apps/server/routers/customer/customerRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ router.get('/customerCards', async (req, res, next) => {
try {
const perProject = await getFileFromS3('perProject')
const employeeCustomers = await getFileFromS3('employeeCustomers')
const accountManager = await getFileFromS3('accountManager')
const project_data = JSON.parse(perProject)
const customer_data = JSON.parse(employeeCustomers)
const aggregatedData = createCustomerCardData(project_data, customer_data)
const accountManagerTable = JSON.parse(accountManager)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legger også til account manager info


const aggregatedData = createCustomerCardData(
project_data,
customer_data,
accountManagerTable
)
res.send(aggregatedData)
} catch (error) {
next(error)
Expand Down
5 changes: 4 additions & 1 deletion apps/server/routers/customer/customerTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export type EmployeeForCustomerListRowData = [

export type CustomerCardsData = {
customer: string
consultants: number
accountManager: string | undefined
consultantsLastPeriod: number
consultantsLastLongPeriod: number
billedLastPeriod: number
billedLastLongPeriod: number
billedTotal: number
}

Expand Down
1 change: 0 additions & 1 deletion apps/server/routers/employees/employeesAggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
WorkExperience,
} from './employeesTypes'
import { EmployeeCustomers } from '../customer/customerTypes'
import { groupBy } from '../../repository/util'

export const aggregateEmployeeTable = async (
basicEmployeeInformation: BasicEmployeeInformation[],
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/api/data/customer/customerApiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { TableRow } from '../tableResponses'
// customerCards
export interface CustomerCardData {
customer: string
consultants: number
accountManager: string | undefined
consultantsLastPeriod: number
consultantsLastLongPeriod: number
billedLastPeriod: number
billedLastLongPeriod: number
billedTotal: number
}

Expand Down
25 changes: 17 additions & 8 deletions apps/web/src/api/data/customer/customerQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import {
getHoursBilledPerCustomerCharts,
getHoursBilledPerWeekCharts,
} from './customerApi'
import { CustomerData } from '../../../pages/customer/cards/CustomerCard'
import { CustomerCardData } from './customerApiTypes'

export const useCustomerCardsQuery = () =>
useSWR('/customerCards', getCustomerCards, {
export const useCustomerCards = () => {
const { data } = useSWR('/customerCards', getCustomerCards, {
revalidateOnFocus: false,
})

export const useCustomerCards = () => {
const { data } = useCustomerCardsQuery()
return data || []
}

Expand All @@ -27,9 +24,21 @@ export const useHoursBilledPerCustomerCharts = () =>
revalidateOnFocus: false,
})

export const useAllCustomerData = (): CustomerData[] => {
export const useAllCustomerData = (): CustomerCardData[] => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vi har ikke antall konsulenter og timer for historiske kunder. Dette burde egentlig fikses. Kansje en forbedring er å hente historisk data sammen med customerCards også heller filtrere ut manglende konsulenter i front

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dette har jeg nå fikset ved å ta med historiske kunder i customerCard apiet og filtrere dem ut senere

const { data: hoursBilledPerCustomer } = useHoursBilledPerCustomerCharts()
return hoursBilledPerCustomer ? hoursBilledPerCustomer?.data : []
return (
hoursBilledPerCustomer?.data?.map(
(cd): CustomerCardData => ({
customer: cd.customer,
accountManager: undefined,
consultantsLastPeriod: 0,
consultantsLastLongPeriod: 0,
billedLastPeriod: 0,
billedLastLongPeriod: 0,
billedTotal: 0,
})
) || []
)
}

export const useHoursBilledPerWeekCharts = () =>
Expand Down
Loading
Loading