@ossy/consultancy 3.27.2 → 3.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -5
- package/src/create-company.flow.js +5 -3
- package/src/current-month.component.jsx +1 -1
- package/src/disable-consultancy.flow.js +3 -3
- package/src/enable-consultancy.flow.js +3 -3
- package/src/get-month-report.action.js +5 -0
- package/src/get-month-report.task.js +69 -0
- package/src/get-month-report.task.spec.js +158 -0
- package/src/index.js +1 -0
- package/src/kpi-trend.component.jsx +1 -1
- package/src/month-report.aggregate.js +70 -0
- package/src/month-report.js +175 -0
- package/src/month-report.spec.js +155 -0
- package/src/overview.page.jsx +1 -1
- package/src/use-chart-months.js +13 -5
- package/src/use-spend-by-month.js +8 -3
- package/src/utilization.component.jsx +1 -1
- package/src/year-totals.component.jsx +1 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/consultancy",
|
|
3
3
|
"description": "Consultancy feature package — schemas, forecasts UI, and assignment margins",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.28.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"module": "./src/index.js",
|
|
@@ -25,11 +25,12 @@
|
|
|
25
25
|
"README.md"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@ossy/email": "^3.
|
|
29
|
-
"@ossy/event-store": "^3.
|
|
28
|
+
"@ossy/email": "^3.28.0",
|
|
29
|
+
"@ossy/event-store": "^3.28.0",
|
|
30
|
+
"@ossy/fold": "^3.26.0",
|
|
30
31
|
"@ossy/organisation": "^3.26.0",
|
|
31
32
|
"@ossy/schema": "^3.27.0",
|
|
32
|
-
"@ossy/spend": "^3.
|
|
33
|
+
"@ossy/spend": "^3.28.0"
|
|
33
34
|
},
|
|
34
35
|
"peerDependencies": {
|
|
35
36
|
"@ossy/calendar": "*",
|
|
@@ -44,5 +45,5 @@
|
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"jest": "^30.2.0"
|
|
46
47
|
},
|
|
47
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "7a3c99ea704300fe2870a33cca13281cd183cd13"
|
|
48
49
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import enableConsultancyFlow from './enable-consultancy.flow.js'
|
|
2
2
|
import { OrganisationSchema, location as organisationLocation } from '@ossy/organisation'
|
|
3
3
|
import { CreateResource, CreateDocumentForm } from '@ossy/resources'
|
|
4
4
|
|
|
@@ -11,13 +11,15 @@ export const metadata = {
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Happy path for issue #5 / #1108 — consultancy empty state to organisation ready.
|
|
14
|
+
* Overview reads GetMonthReport (entitlement-gated), so enable first — same as
|
|
15
|
+
* view-forecasts. ListResources used to allow this path without enable.
|
|
14
16
|
*/
|
|
15
17
|
export default {
|
|
16
18
|
title: 'Create company',
|
|
17
19
|
description:
|
|
18
|
-
'Signed-in user
|
|
20
|
+
'Signed-in user enables consultancy, sees overview empty, creates organisation via create-document, then sees the ready overview',
|
|
19
21
|
steps: [
|
|
20
|
-
...
|
|
22
|
+
...enableConsultancyFlow.steps,
|
|
21
23
|
{ page: '@consultancy/overview' },
|
|
22
24
|
{ result: { selector: '[data-consultancy-status="empty"]', timeout: 15000 } },
|
|
23
25
|
{
|
|
@@ -73,5 +73,5 @@ export const metadata = {
|
|
|
73
73
|
|
|
74
74
|
/** Slot widget — current-month forecast panel for dashboards. */
|
|
75
75
|
export default function CurrentMonthForecastWidget (props) {
|
|
76
|
-
return <DataLoader emptyGuide={false} content={<CurrentMonthForecastPanel {...props} />} />
|
|
76
|
+
return <DataLoader emptyGuide={false} source="projection" content={<CurrentMonthForecastPanel {...props} />} />
|
|
77
77
|
}
|
|
@@ -11,16 +11,16 @@ export const metadata = {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* After enabling consultancy, disable from the
|
|
14
|
+
* After enabling consultancy, disable from the store detail.
|
|
15
15
|
* Consultancy has no sales home — assert EnableService returns on the catalog.
|
|
16
16
|
*/
|
|
17
17
|
export default {
|
|
18
18
|
title: 'Disable consultancy',
|
|
19
19
|
description:
|
|
20
|
-
'Signed-in user enables consultancy, disables it from the
|
|
20
|
+
'Signed-in user enables consultancy, disables it from the store, and sees Enable again',
|
|
21
21
|
steps: [
|
|
22
22
|
...enableConsultancyFlow.steps,
|
|
23
|
-
{ page: '@
|
|
23
|
+
{ page: '@marketplace/detail', params: { packageSlug: 'consultancy' } },
|
|
24
24
|
{
|
|
25
25
|
result: {
|
|
26
26
|
action: { ...DisableService, service: CONSULTANCY },
|
|
@@ -10,8 +10,8 @@ export const metadata = {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Consultancy has no sales home — enable from the
|
|
14
|
-
* Uses
|
|
13
|
+
* Consultancy has no sales home — enable from the store detail.
|
|
14
|
+
* Uses the workspace's enabled services.
|
|
15
15
|
*/
|
|
16
16
|
export default {
|
|
17
17
|
title: 'Enable consultancy',
|
|
@@ -19,7 +19,7 @@ export default {
|
|
|
19
19
|
'New user registers, opens the consultancy package detail, and enables the workspace service',
|
|
20
20
|
steps: [
|
|
21
21
|
...signUpFlow.steps,
|
|
22
|
-
{ page: '@
|
|
22
|
+
{ page: '@marketplace/detail', params: { packageSlug: 'consultancy' } },
|
|
23
23
|
{
|
|
24
24
|
result: {
|
|
25
25
|
action: { ...EnableService, service: CONSULTANCY },
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { location as consultancyLocation } from './locations.js'
|
|
2
|
+
import { location as organisationLocation } from '@ossy/organisation'
|
|
3
|
+
import { location as spendLocation } from '@ossy/spend/locations'
|
|
4
|
+
import { getProjection, saveProjection } from '@ossy/event-store'
|
|
5
|
+
import { MonthReportProjection } from './month-report.aggregate.js'
|
|
6
|
+
import { TIMESHEET_LOCATION } from './utilization-hours.js'
|
|
7
|
+
import {
|
|
8
|
+
localMonthKey,
|
|
9
|
+
fillReport,
|
|
10
|
+
projectReport,
|
|
11
|
+
recordsFromSnapshots,
|
|
12
|
+
shapeMonthReport,
|
|
13
|
+
} from './month-report.js'
|
|
14
|
+
|
|
15
|
+
export const metadata = { id: '@ossy/consultancy/tasks/get-month-report' }
|
|
16
|
+
|
|
17
|
+
const ListResources = { id: '@ossy/resources/actions/list' }
|
|
18
|
+
|
|
19
|
+
async function loadSnapshots (sdk, req, workspaceId) {
|
|
20
|
+
const user = req?.user
|
|
21
|
+
const payload = {
|
|
22
|
+
workspaceId,
|
|
23
|
+
...(user ? { user } : {}),
|
|
24
|
+
}
|
|
25
|
+
const lists = await Promise.all([
|
|
26
|
+
sdk.invoke(ListResources, { ...payload, location: consultancyLocation }),
|
|
27
|
+
sdk.invoke(ListResources, { ...payload, location: organisationLocation }),
|
|
28
|
+
sdk.invoke(ListResources, { ...payload, location: spendLocation }),
|
|
29
|
+
sdk.invoke(ListResources, { ...payload, location: TIMESHEET_LOCATION }),
|
|
30
|
+
])
|
|
31
|
+
return lists.flatMap((list) => (Array.isArray(list) ? list : []))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function run ({ payload, req, sdk }) {
|
|
35
|
+
const workspaceId = payload?.workspaceId ?? req?.workspaceId
|
|
36
|
+
if (!workspaceId) {
|
|
37
|
+
throw Object.assign(new Error('workspaceId required'), { status: 400 })
|
|
38
|
+
}
|
|
39
|
+
if (!sdk?.invoke) {
|
|
40
|
+
throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let state = await getProjection(workspaceId, MonthReportProjection.ProjectionId)
|
|
44
|
+
const now = new Date()
|
|
45
|
+
|
|
46
|
+
if (!state?.backfilled) {
|
|
47
|
+
const existingVersion = Number(state?.version) > 0 ? Number(state.version) : 0
|
|
48
|
+
// ListResources is the full current folder view. Overlay any records the
|
|
49
|
+
// changestream already folded so a concurrent Apply is not dropped.
|
|
50
|
+
const listed = recordsFromSnapshots(await loadSnapshots(sdk, req, workspaceId))
|
|
51
|
+
const records = { ...listed, ...(state?.records ?? {}) }
|
|
52
|
+
state = fillReport({
|
|
53
|
+
workspaceId: state?.workspaceId ?? workspaceId,
|
|
54
|
+
records,
|
|
55
|
+
months: {},
|
|
56
|
+
})
|
|
57
|
+
await saveProjection(workspaceId, MonthReportProjection.ProjectionId, state, {
|
|
58
|
+
version: existingVersion + 1,
|
|
59
|
+
})
|
|
60
|
+
} else if (!state.months?.[localMonthKey(now)] || state.sealedAt) {
|
|
61
|
+
const existingVersion = Number(state?.version) > 0 ? Number(state.version) : 1
|
|
62
|
+
state = projectReport(state)
|
|
63
|
+
await saveProjection(workspaceId, MonthReportProjection.ProjectionId, state, {
|
|
64
|
+
version: existingVersion,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return shapeMonthReport(state)
|
|
69
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
|
|
2
|
+
|
|
3
|
+
const getProjection = jest.fn()
|
|
4
|
+
const saveProjection = jest.fn()
|
|
5
|
+
|
|
6
|
+
jest.unstable_mockModule('@ossy/event-store', () => ({
|
|
7
|
+
getProjection,
|
|
8
|
+
saveProjection,
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
const { run } = await import('./get-month-report.task.js')
|
|
12
|
+
const { localMonthKey } = await import('./month-report.js')
|
|
13
|
+
|
|
14
|
+
describe('get-month-report task', () => {
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
getProjection.mockReset()
|
|
17
|
+
saveProjection.mockReset()
|
|
18
|
+
saveProjection.mockResolvedValue(undefined)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('requires a workspace', async () => {
|
|
22
|
+
await expect(run({ payload: {}, req: {}, sdk: { invoke: jest.fn() } }))
|
|
23
|
+
.rejects.toMatchObject({ status: 400 })
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('reads a backfilled snapshot without listing folders', async () => {
|
|
27
|
+
const month = localMonthKey()
|
|
28
|
+
const [year, monthNumber] = month.split('-')
|
|
29
|
+
getProjection.mockResolvedValue({
|
|
30
|
+
workspaceId: 'ws-1',
|
|
31
|
+
backfilled: true,
|
|
32
|
+
records: {},
|
|
33
|
+
months: {
|
|
34
|
+
[month]: {
|
|
35
|
+
year: Number(year),
|
|
36
|
+
monthNumber,
|
|
37
|
+
projectedIncome: 10,
|
|
38
|
+
monthlyCostOfEmployees: 0,
|
|
39
|
+
revenue: 10,
|
|
40
|
+
hoursWorked: 1,
|
|
41
|
+
spend: 2,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
const sdk = { invoke: jest.fn() }
|
|
46
|
+
|
|
47
|
+
const result = await run({
|
|
48
|
+
payload: { workspaceId: 'ws-1' },
|
|
49
|
+
req: {},
|
|
50
|
+
sdk,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
expect(sdk.invoke).not.toHaveBeenCalled()
|
|
54
|
+
expect(saveProjection).not.toHaveBeenCalled()
|
|
55
|
+
expect(result.months).toEqual([
|
|
56
|
+
expect.objectContaining({ projectedIncome: 10, spend: 2 }),
|
|
57
|
+
])
|
|
58
|
+
expect(result.hasSetup).toBe(false)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('recalculates a snapshot that was sealed', async () => {
|
|
62
|
+
const month = localMonthKey()
|
|
63
|
+
const [year, monthNumber] = month.split('-')
|
|
64
|
+
getProjection.mockResolvedValue({
|
|
65
|
+
workspaceId: 'ws-1',
|
|
66
|
+
version: 2,
|
|
67
|
+
backfilled: true,
|
|
68
|
+
sealedAt: 1,
|
|
69
|
+
records: {},
|
|
70
|
+
months: {
|
|
71
|
+
[month]: {
|
|
72
|
+
year: Number(year),
|
|
73
|
+
monthNumber,
|
|
74
|
+
projectedIncome: 5,
|
|
75
|
+
sealed: true,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
await run({
|
|
81
|
+
payload: { workspaceId: 'ws-1' },
|
|
82
|
+
req: {},
|
|
83
|
+
sdk: { invoke: jest.fn() },
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
const saved = saveProjection.mock.calls[0][2]
|
|
87
|
+
expect(saved.sealedAt).toBeUndefined()
|
|
88
|
+
expect(saved.months[month]?.sealed).toBeUndefined()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('fills the snapshot from current resources once', async () => {
|
|
92
|
+
getProjection.mockResolvedValue(null)
|
|
93
|
+
const sdk = { invoke: jest.fn().mockResolvedValue([]) }
|
|
94
|
+
|
|
95
|
+
const result = await run({
|
|
96
|
+
payload: { workspaceId: 'ws-1' },
|
|
97
|
+
req: { user: { id: 'u-1' } },
|
|
98
|
+
sdk,
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
expect(sdk.invoke).toHaveBeenCalledTimes(4)
|
|
102
|
+
expect(saveProjection).toHaveBeenCalledWith(
|
|
103
|
+
'ws-1',
|
|
104
|
+
'@ossy/consultancy/data/month-report',
|
|
105
|
+
expect.objectContaining({ backfilled: true, workspaceId: 'ws-1' }),
|
|
106
|
+
{ version: 1 },
|
|
107
|
+
)
|
|
108
|
+
expect(result.hasSetup).toBe(false)
|
|
109
|
+
expect(result.months).toEqual([])
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('keeps live projection records and bumps the stored version on first fill', async () => {
|
|
113
|
+
getProjection.mockResolvedValue({
|
|
114
|
+
workspaceId: 'ws-1',
|
|
115
|
+
version: 4,
|
|
116
|
+
records: {
|
|
117
|
+
'emp-live': {
|
|
118
|
+
id: 'emp-live',
|
|
119
|
+
type: '@ossy/consultancy/schema/employee',
|
|
120
|
+
content: { name: 'Live' },
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
months: {},
|
|
124
|
+
})
|
|
125
|
+
const sdk = {
|
|
126
|
+
invoke: jest.fn().mockResolvedValue([
|
|
127
|
+
{
|
|
128
|
+
id: 'emp-list',
|
|
129
|
+
type: '@ossy/consultancy/schema/employee',
|
|
130
|
+
content: { name: 'Listed' },
|
|
131
|
+
},
|
|
132
|
+
]),
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const result = await run({
|
|
136
|
+
payload: { workspaceId: 'ws-1' },
|
|
137
|
+
req: {},
|
|
138
|
+
sdk,
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
expect(saveProjection).toHaveBeenCalledWith(
|
|
142
|
+
'ws-1',
|
|
143
|
+
'@ossy/consultancy/data/month-report',
|
|
144
|
+
expect.objectContaining({
|
|
145
|
+
backfilled: true,
|
|
146
|
+
records: expect.objectContaining({
|
|
147
|
+
'emp-list': expect.objectContaining({ id: 'emp-list' }),
|
|
148
|
+
'emp-live': expect.objectContaining({ id: 'emp-live' }),
|
|
149
|
+
}),
|
|
150
|
+
}),
|
|
151
|
+
{ version: 5 },
|
|
152
|
+
)
|
|
153
|
+
expect(result.employees).toEqual(expect.arrayContaining([
|
|
154
|
+
expect.objectContaining({ id: 'emp-live' }),
|
|
155
|
+
expect.objectContaining({ id: 'emp-list' }),
|
|
156
|
+
]))
|
|
157
|
+
})
|
|
158
|
+
})
|
package/src/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { default as requestSchema } from './request.schema.js'
|
|
|
8
8
|
export { default as proposalSchema } from './proposal.schema.js'
|
|
9
9
|
export { Definition } from './Definition.js'
|
|
10
10
|
export { CreateConsultancyButton } from './CreateConsultancyButton.jsx'
|
|
11
|
+
export { metadata as GetMonthReport } from './get-month-report.action.js'
|
|
11
12
|
export {
|
|
12
13
|
SOCIAL_TAX,
|
|
13
14
|
SEMESTER_TILLAGG_ANNUAL_RATE,
|
|
@@ -13,5 +13,5 @@ export const metadata = {
|
|
|
13
13
|
|
|
14
14
|
/** Slot widget — fiscal-year KPI trend for dashboards. */
|
|
15
15
|
export default function KpiTrendWidget (props) {
|
|
16
|
-
return <DataLoader emptyGuide={false} content={<KpiTrendPanel {...props} />} />
|
|
16
|
+
return <DataLoader emptyGuide={false} source="projection" content={<KpiTrendPanel {...props} />} />
|
|
17
17
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ConsultancySchema } from './schema-ids.js'
|
|
2
|
+
import { OrganisationSchema } from '@ossy/organisation'
|
|
3
|
+
import { SpendSchema } from '@ossy/spend/schema-ids'
|
|
4
|
+
import { TIMESHEET_TYPE } from './utilization-hours.js'
|
|
5
|
+
import { applyRecordEvent, fillReport, projectReport } from './month-report.js'
|
|
6
|
+
|
|
7
|
+
const RESOURCE_EVENTS = ['Created', 'Patched', 'Updated', 'Deleted']
|
|
8
|
+
const SOURCE_TYPES = [
|
|
9
|
+
ConsultancySchema.employee,
|
|
10
|
+
ConsultancySchema.contract,
|
|
11
|
+
ConsultancySchema.vacation,
|
|
12
|
+
OrganisationSchema.organisation,
|
|
13
|
+
SpendSchema.purchase,
|
|
14
|
+
TIMESHEET_TYPE,
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* One workspace snapshot of the consultancy overview.
|
|
19
|
+
*
|
|
20
|
+
* Records are the inputs (people, contracts, vacations, organisation,
|
|
21
|
+
* SEK purchases, timesheets). `months` is the open fiscal year, recalculated
|
|
22
|
+
* from those records whenever one of them changes.
|
|
23
|
+
*
|
|
24
|
+
* The first overview read fills this from the current resources when
|
|
25
|
+
* `backfilled` is still unset. A projection rebuild calls `settle`, which
|
|
26
|
+
* is the same calculation.
|
|
27
|
+
*/
|
|
28
|
+
export class MonthReportProjection {
|
|
29
|
+
|
|
30
|
+
static kind = 'projection'
|
|
31
|
+
static ProjectionId = '@ossy/consultancy/data/month-report'
|
|
32
|
+
|
|
33
|
+
/** Live patches often omit belongsTo. Dispatch looks the record up. */
|
|
34
|
+
static trackResources = true
|
|
35
|
+
|
|
36
|
+
static sources = SOURCE_TYPES.flatMap((type) => (
|
|
37
|
+
RESOURCE_EVENTS.map((event) => ({ type, event }))
|
|
38
|
+
))
|
|
39
|
+
|
|
40
|
+
static scopeFromEvent (event) {
|
|
41
|
+
const workspaceId = event?.payload?.belongsTo ?? event?.belongsTo
|
|
42
|
+
return typeof workspaceId === 'string' && workspaceId ? workspaceId : null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static cacheKeys (scopeId) {
|
|
46
|
+
return [
|
|
47
|
+
`projection:${MonthReportProjection.ProjectionId}:${scopeId}`,
|
|
48
|
+
'action:@ossy/consultancy/actions/get-month-report',
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static initialState (scopeId) {
|
|
53
|
+
return { workspaceId: scopeId, records: {}, months: {} }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static Apply (event, state) {
|
|
57
|
+
const next = applyRecordEvent(state, event)
|
|
58
|
+
// A rebuild folds records first. Months are calculated once it is filled,
|
|
59
|
+
// and again on every later change.
|
|
60
|
+
if (!next.backfilled) return next
|
|
61
|
+
return projectReport(next)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static settle (state) {
|
|
65
|
+
return fillReport(state)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { MonthReportProjection as Aggregate }
|
|
70
|
+
export const id = 'consultancy/data/month-report'
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { fold } from '@ossy/fold'
|
|
2
|
+
import { CompanyClient } from '@ossy/calendar/company-client'
|
|
3
|
+
import { ForecastClient } from '@ossy/calendar/forecast-client'
|
|
4
|
+
import { resourceIdOf } from '@ossy/calendar/holiday-locale'
|
|
5
|
+
import { OrganisationSchema } from '@ossy/organisation'
|
|
6
|
+
import { SpendSchema } from '@ossy/spend/schema-ids'
|
|
7
|
+
import { buildSpendStats } from '@ossy/spend/spend-stats'
|
|
8
|
+
import { ConsultancySchema } from './schema-ids.js'
|
|
9
|
+
import { TIMESHEET_TYPE } from './utilization-hours.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Local calendar month, matching the overview grid.
|
|
13
|
+
* @param {Date} [date]
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export function localMonthKey (date = new Date()) {
|
|
17
|
+
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {Record<string, object>} records
|
|
22
|
+
* @param {string} type
|
|
23
|
+
*/
|
|
24
|
+
function resourcesOfType (records, type) {
|
|
25
|
+
return Object.values(records ?? {}).filter((resource) => resource?.type === type)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function withContentId (resource) {
|
|
29
|
+
return { ...(resource?.content ?? {}), id: resource?.id }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Company inputs the forecast already uses, taken from stored records.
|
|
34
|
+
* @param {Record<string, object>} records
|
|
35
|
+
*/
|
|
36
|
+
export function companyFromRecords (records) {
|
|
37
|
+
const employees = resourcesOfType(records, ConsultancySchema.employee).map(withContentId)
|
|
38
|
+
const vacations = resourcesOfType(records, ConsultancySchema.vacation).map(withContentId)
|
|
39
|
+
const contracts = resourcesOfType(records, ConsultancySchema.contract).map(withContentId)
|
|
40
|
+
const organisation = resourcesOfType(records, OrganisationSchema.organisation)[0]
|
|
41
|
+
return CompanyClient.of({
|
|
42
|
+
basicInfo: organisation?.content ?? {},
|
|
43
|
+
contracts,
|
|
44
|
+
vacations,
|
|
45
|
+
employees: employees.map((employee) => ({
|
|
46
|
+
...employee,
|
|
47
|
+
vacations: vacations.filter((vacation) => resourceIdOf(vacation.employeeId) === employee.id),
|
|
48
|
+
})),
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Fold one resource event into the report's record map.
|
|
54
|
+
* A deleted record is dropped. Later events for it find the workspace only
|
|
55
|
+
* while an earlier copy is still stored.
|
|
56
|
+
* @param {object} state
|
|
57
|
+
* @param {object} event
|
|
58
|
+
*/
|
|
59
|
+
export function applyRecordEvent (state, event) {
|
|
60
|
+
const records = { ...(state?.records ?? {}) }
|
|
61
|
+
const id = event?.resourceId
|
|
62
|
+
if (!id) return { ...state, records }
|
|
63
|
+
|
|
64
|
+
if (event.event === 'Deleted') {
|
|
65
|
+
delete records[id]
|
|
66
|
+
return {
|
|
67
|
+
...state,
|
|
68
|
+
workspaceId: event.payload?.belongsTo ?? state.workspaceId,
|
|
69
|
+
records,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
records[id] = fold(records[id] ?? {}, event)
|
|
74
|
+
return {
|
|
75
|
+
...state,
|
|
76
|
+
workspaceId: event.payload?.belongsTo ?? state.workspaceId,
|
|
77
|
+
records,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Recompute the open fiscal year from the stored records.
|
|
83
|
+
* Every month is rewritten, including earlier months in the year.
|
|
84
|
+
* Months outside that year are dropped. Spend is the SEK invoice total.
|
|
85
|
+
* @param {object} state
|
|
86
|
+
*/
|
|
87
|
+
export function projectReport (state) {
|
|
88
|
+
const company = companyFromRecords(state?.records)
|
|
89
|
+
const holidayLocale = company.holidayLocale || 'SE'
|
|
90
|
+
const forecast = company.currentFiscalYearStart
|
|
91
|
+
? ForecastClient.of(company).getFiscalYear({ holidayLocale, language: 'en' })
|
|
92
|
+
: []
|
|
93
|
+
const purchases = resourcesOfType(state?.records, SpendSchema.purchase)
|
|
94
|
+
const spend = buildSpendStats(purchases, {
|
|
95
|
+
fiscalYearStart: company.currentFiscalYearStart,
|
|
96
|
+
fiscalYearEnd: company.currentFiscalYearEnd,
|
|
97
|
+
currency: 'SEK',
|
|
98
|
+
})
|
|
99
|
+
const spendByMonth = spend.byMonth ?? {}
|
|
100
|
+
const months = {}
|
|
101
|
+
|
|
102
|
+
for (const month of forecast) {
|
|
103
|
+
const monthNumber = String(month.monthNumber).padStart(2, '0')
|
|
104
|
+
const key = `${month.year}-${monthNumber}`
|
|
105
|
+
months[key] = {
|
|
106
|
+
year: month.year,
|
|
107
|
+
monthNumber,
|
|
108
|
+
projectedIncome: month.projectedIncome || 0,
|
|
109
|
+
monthlyCostOfEmployees: month.monthlyCostOfEmployees || 0,
|
|
110
|
+
revenue: month.revenue || 0,
|
|
111
|
+
hoursWorked: month.hoursWorked || 0,
|
|
112
|
+
spend: Number(spendByMonth[key]) || 0,
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const next = { ...state, months }
|
|
117
|
+
delete next.sealedAt
|
|
118
|
+
return next
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Calculate the fiscal year and mark the snapshot as filled from current records.
|
|
123
|
+
* The next overview read uses this document.
|
|
124
|
+
* @param {object} state
|
|
125
|
+
*/
|
|
126
|
+
export function fillReport (state) {
|
|
127
|
+
return { ...projectReport(state), backfilled: true }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {Array<object>} resources
|
|
132
|
+
*/
|
|
133
|
+
export function recordsFromSnapshots (resources) {
|
|
134
|
+
const records = {}
|
|
135
|
+
for (const resource of Array.isArray(resources) ? resources : []) {
|
|
136
|
+
if (!resource?.id || !resource.type) continue
|
|
137
|
+
if (Array.isArray(resource.status) && resource.status.includes('removed')) continue
|
|
138
|
+
records[resource.id] = {
|
|
139
|
+
id: resource.id,
|
|
140
|
+
resourceId: resource.id,
|
|
141
|
+
type: resource.type,
|
|
142
|
+
belongsTo: resource.belongsTo,
|
|
143
|
+
location: resource.location,
|
|
144
|
+
content: resource.content && typeof resource.content === 'object' ? resource.content : {},
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return records
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Read model for the overview. Day grids stay on the client.
|
|
152
|
+
* @param {object} state
|
|
153
|
+
*/
|
|
154
|
+
export function shapeMonthReport (state) {
|
|
155
|
+
const records = state?.records ?? {}
|
|
156
|
+
const employees = resourcesOfType(records, ConsultancySchema.employee)
|
|
157
|
+
const contracts = resourcesOfType(records, ConsultancySchema.contract)
|
|
158
|
+
const vacations = resourcesOfType(records, ConsultancySchema.vacation)
|
|
159
|
+
const timesheets = resourcesOfType(records, TIMESHEET_TYPE)
|
|
160
|
+
const organisation = resourcesOfType(records, OrganisationSchema.organisation)[0]
|
|
161
|
+
const months = Object.entries(state?.months ?? {})
|
|
162
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
163
|
+
.map(([, month]) => month)
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
workspaceId: state?.workspaceId ?? null,
|
|
167
|
+
hasSetup: employees.length + contracts.length + vacations.length > 0 || Boolean(organisation),
|
|
168
|
+
basicInfo: organisation?.content ?? {},
|
|
169
|
+
employees,
|
|
170
|
+
contracts,
|
|
171
|
+
vacations,
|
|
172
|
+
timesheets,
|
|
173
|
+
months,
|
|
174
|
+
}
|
|
175
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals'
|
|
2
|
+
import { ConsultancySchema } from './schema-ids.js'
|
|
3
|
+
import { OrganisationSchema } from '@ossy/organisation'
|
|
4
|
+
import { SpendSchema } from '@ossy/spend/schema-ids'
|
|
5
|
+
import {
|
|
6
|
+
applyRecordEvent,
|
|
7
|
+
localMonthKey,
|
|
8
|
+
fillReport,
|
|
9
|
+
projectReport,
|
|
10
|
+
recordsFromSnapshots,
|
|
11
|
+
shapeMonthReport,
|
|
12
|
+
} from './month-report.js'
|
|
13
|
+
|
|
14
|
+
const now = new Date(2026, 10, 15)
|
|
15
|
+
|
|
16
|
+
function record (id, type, content) {
|
|
17
|
+
return { id, type, content }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function companyRecords () {
|
|
21
|
+
return {
|
|
22
|
+
org: record('org', OrganisationSchema.organisation, {
|
|
23
|
+
name: 'Ossy',
|
|
24
|
+
registrationDate: '2026-01-01',
|
|
25
|
+
financialYearStart: '0',
|
|
26
|
+
financialYearEnd: '11',
|
|
27
|
+
holidayLocale: 'SE',
|
|
28
|
+
}),
|
|
29
|
+
'emp-1': record('emp-1', ConsultancySchema.employee, {
|
|
30
|
+
name: 'Ada',
|
|
31
|
+
monthlySalary: 0,
|
|
32
|
+
workHoursPerDay: 8,
|
|
33
|
+
startDate: Date.parse('2026-01-01T00:00:00.000Z'),
|
|
34
|
+
}),
|
|
35
|
+
'c-1': record('c-1', ConsultancySchema.contract, {
|
|
36
|
+
employeeId: { resourceId: 'emp-1' },
|
|
37
|
+
hourlyCompensation: 1000,
|
|
38
|
+
startDate: Date.parse('2026-01-01T00:00:00.000Z'),
|
|
39
|
+
}),
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('month report', () => {
|
|
44
|
+
it('recomputes every fiscal-year month when a contract rate changes', () => {
|
|
45
|
+
const filled = fillReport({ workspaceId: 'ws-1', records: companyRecords(), months: {} })
|
|
46
|
+
const january = filled.months['2026-01']
|
|
47
|
+
const november = filled.months['2026-11']
|
|
48
|
+
|
|
49
|
+
expect(filled.backfilled).toBe(true)
|
|
50
|
+
expect(january.projectedIncome).toBeGreaterThan(0)
|
|
51
|
+
expect(november.projectedIncome).toBeGreaterThan(0)
|
|
52
|
+
expect(january.sealed).toBeUndefined()
|
|
53
|
+
|
|
54
|
+
const raised = projectReport({
|
|
55
|
+
...filled,
|
|
56
|
+
records: {
|
|
57
|
+
...filled.records,
|
|
58
|
+
'c-1': record('c-1', ConsultancySchema.contract, {
|
|
59
|
+
...filled.records['c-1'].content,
|
|
60
|
+
hourlyCompensation: 2000,
|
|
61
|
+
}),
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
expect(raised.months['2026-01'].projectedIncome).toBeGreaterThan(january.projectedIncome)
|
|
66
|
+
expect(raised.months['2026-11'].projectedIncome).toBeGreaterThan(november.projectedIncome)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('adds a purchase to the month of its invoice date', () => {
|
|
70
|
+
const filled = fillReport({ workspaceId: 'ws-1', records: companyRecords(), months: {} })
|
|
71
|
+
const withPurchase = projectReport({
|
|
72
|
+
...filled,
|
|
73
|
+
records: {
|
|
74
|
+
...filled.records,
|
|
75
|
+
bill: record('bill', SpendSchema.purchase, {
|
|
76
|
+
supplier: 'Office',
|
|
77
|
+
total: 100,
|
|
78
|
+
currency: 'SEK',
|
|
79
|
+
issueDate: '2026-01-15',
|
|
80
|
+
}),
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
expect(withPurchase.months['2026-01'].spend).toBe(100)
|
|
85
|
+
expect(withPurchase.months['2026-01'].projectedIncome).toBe(filled.months['2026-01'].projectedIncome)
|
|
86
|
+
expect(withPurchase.months['2026-11'].spend).toBe(0)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('folds a patch onto the stored record and drops deletes', () => {
|
|
90
|
+
const created = applyRecordEvent({ records: {} }, {
|
|
91
|
+
resourceId: 'c-1',
|
|
92
|
+
type: ConsultancySchema.contract,
|
|
93
|
+
event: 'Created',
|
|
94
|
+
payload: {
|
|
95
|
+
belongsTo: 'ws-1',
|
|
96
|
+
content: { hourlyCompensation: 1000 },
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
const patched = applyRecordEvent(created, {
|
|
100
|
+
resourceId: 'c-1',
|
|
101
|
+
type: ConsultancySchema.contract,
|
|
102
|
+
event: 'Patched',
|
|
103
|
+
payload: { content: { hourlyCompensation: 1500 } },
|
|
104
|
+
})
|
|
105
|
+
expect(patched.records['c-1'].content.hourlyCompensation).toBe(1500)
|
|
106
|
+
expect(patched.workspaceId).toBe('ws-1')
|
|
107
|
+
|
|
108
|
+
const removed = applyRecordEvent(patched, {
|
|
109
|
+
resourceId: 'c-1',
|
|
110
|
+
type: ConsultancySchema.contract,
|
|
111
|
+
event: 'Deleted',
|
|
112
|
+
payload: {},
|
|
113
|
+
})
|
|
114
|
+
expect(removed.records['c-1']).toBeUndefined()
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('shapes the overview read model from stored records', () => {
|
|
118
|
+
const state = fillReport({ workspaceId: 'ws-1', records: companyRecords(), months: {} })
|
|
119
|
+
const shape = shapeMonthReport(state)
|
|
120
|
+
expect(shape.hasSetup).toBe(true)
|
|
121
|
+
expect(shape.employees).toHaveLength(1)
|
|
122
|
+
expect(shape.contracts).toHaveLength(1)
|
|
123
|
+
expect(shape.basicInfo.holidayLocale).toBe('SE')
|
|
124
|
+
expect(shape.months.some((month) => month.monthNumber === '11')).toBe(true)
|
|
125
|
+
expect(localMonthKey(now)).toBe('2026-11')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('skips removed snapshots when filling records', () => {
|
|
129
|
+
const records = recordsFromSnapshots([
|
|
130
|
+
{ id: 'gone', type: ConsultancySchema.employee, status: ['removed'], content: { name: 'Gone' } },
|
|
131
|
+
{ id: 'emp-1', type: ConsultancySchema.employee, content: { name: 'Ada' } },
|
|
132
|
+
])
|
|
133
|
+
expect(records['gone']).toBeUndefined()
|
|
134
|
+
expect(records['emp-1'].content.name).toBe('Ada')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('drops months outside the current fiscal-year forecast', () => {
|
|
138
|
+
const filled = fillReport({ workspaceId: 'ws-1', records: companyRecords(), months: {} })
|
|
139
|
+
filled.months['2025-06'] = {
|
|
140
|
+
year: 2025,
|
|
141
|
+
monthNumber: '06',
|
|
142
|
+
projectedIncome: 999,
|
|
143
|
+
monthlyCostOfEmployees: 0,
|
|
144
|
+
revenue: 999,
|
|
145
|
+
hoursWorked: 1,
|
|
146
|
+
spend: 0,
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const next = projectReport(filled)
|
|
150
|
+
expect(next.months['2025-06']).toBeUndefined()
|
|
151
|
+
expect(Object.keys(next.months)).toHaveLength(12)
|
|
152
|
+
expect(next.months['2026-01'].projectedIncome).toBe(filled.months['2026-01'].projectedIncome)
|
|
153
|
+
expect(next.months['2026-01'].sealed).toBeUndefined()
|
|
154
|
+
})
|
|
155
|
+
})
|
package/src/overview.page.jsx
CHANGED
package/src/use-chart-months.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from 'react'
|
|
2
|
-
import { useContracts, useEmployees, useForecast, useVacations } from '@ossy/calendar'
|
|
2
|
+
import { useContracts, useData, useEmployees, useForecast, useVacations } from '@ossy/calendar'
|
|
3
3
|
import { ListResources } from '@ossy/resources'
|
|
4
4
|
import { useSdk } from '@ossy/sdk-react'
|
|
5
5
|
import { attachSpend } from './spend-for-month.js'
|
|
@@ -15,16 +15,24 @@ export function useChartMonths () {
|
|
|
15
15
|
const contracts = useContracts()
|
|
16
16
|
const employees = useEmployees()
|
|
17
17
|
const vacations = useVacations()
|
|
18
|
+
const projectedSheets = useData()?.timesheets
|
|
19
|
+
const useProjected = Array.isArray(projectedSheets)
|
|
18
20
|
const sdk = useSdk()
|
|
19
|
-
const { data: timesheetResources = [] } = sdk.read(
|
|
21
|
+
const { data: timesheetResources = [] } = sdk.read(
|
|
22
|
+
ListResources,
|
|
23
|
+
{ location: TIMESHEET_LOCATION },
|
|
24
|
+
{ enabled: !useProjected },
|
|
25
|
+
)
|
|
20
26
|
const { totals: spendByMonth } = useSpendByMonth()
|
|
21
27
|
|
|
22
28
|
return useMemo(() => attachSpend(withUtilizationHours({
|
|
23
29
|
months,
|
|
24
|
-
timesheets:
|
|
25
|
-
|
|
30
|
+
timesheets: useProjected
|
|
31
|
+
? projectedSheets
|
|
32
|
+
: (Array.isArray(timesheetResources) ? timesheetResources : [])
|
|
33
|
+
.filter((resource) => resource.type === TIMESHEET_TYPE),
|
|
26
34
|
vacations,
|
|
27
35
|
employees,
|
|
28
36
|
contracts,
|
|
29
|
-
}), spendByMonth), [months, timesheetResources, vacations, employees, contracts, spendByMonth])
|
|
37
|
+
}), spendByMonth), [months, timesheetResources, projectedSheets, useProjected, vacations, employees, contracts, spendByMonth])
|
|
30
38
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from 'react'
|
|
2
|
-
import { useCompany } from '@ossy/calendar'
|
|
2
|
+
import { useCompany, useData } from '@ossy/calendar'
|
|
3
3
|
import { ListResources } from '@ossy/resources'
|
|
4
4
|
import { useSdk } from '@ossy/sdk-react'
|
|
5
5
|
import { buildSpendStats, calendarDayFromIssueDate, location, purchaseInFiscalSpan, SpendSchema } from '@ossy/spend'
|
|
@@ -12,12 +12,17 @@ const CHART_CURRENCY = 'SEK'
|
|
|
12
12
|
*/
|
|
13
13
|
export function useSpendByMonth () {
|
|
14
14
|
const company = useCompany()
|
|
15
|
+
const projected = useData()?.spendByMonth
|
|
16
|
+
const useProjected = projected instanceof Map
|
|
15
17
|
const sdk = useSdk()
|
|
16
|
-
const { data: resources = [] } = sdk.read(ListResources, { location })
|
|
18
|
+
const { data: resources = [] } = sdk.read(ListResources, { location }, { enabled: !useProjected })
|
|
17
19
|
const fiscalYearStart = company?.currentFiscalYearStart ?? null
|
|
18
20
|
const fiscalYearEnd = company?.currentFiscalYearEnd ?? null
|
|
19
21
|
|
|
20
22
|
return useMemo(() => {
|
|
23
|
+
if (useProjected) {
|
|
24
|
+
return { totals: projected, purchasesByMonth: new Map() }
|
|
25
|
+
}
|
|
21
26
|
const purchases = (Array.isArray(resources) ? resources : [])
|
|
22
27
|
.filter((resource) => resource?.type === SpendSchema.purchase)
|
|
23
28
|
const stats = buildSpendStats(purchases, {
|
|
@@ -48,5 +53,5 @@ export function useSpendByMonth () {
|
|
|
48
53
|
list.sort((a, b) => b.total - a.total)
|
|
49
54
|
}
|
|
50
55
|
return { totals, purchasesByMonth }
|
|
51
|
-
}, [resources, fiscalYearStart, fiscalYearEnd])
|
|
56
|
+
}, [resources, fiscalYearStart, fiscalYearEnd, projected, useProjected])
|
|
52
57
|
}
|
|
@@ -13,5 +13,5 @@ export const metadata = {
|
|
|
13
13
|
|
|
14
14
|
/** Slot widget — fiscal-year utilization for dashboards. */
|
|
15
15
|
export default function UtilizationWidget (props) {
|
|
16
|
-
return <DataLoader emptyGuide={false} content={<UtilizationPanel {...props} />} />
|
|
16
|
+
return <DataLoader emptyGuide={false} source="projection" content={<UtilizationPanel {...props} />} />
|
|
17
17
|
}
|
|
@@ -13,5 +13,5 @@ export const metadata = {
|
|
|
13
13
|
|
|
14
14
|
/** Slot widget — fiscal-year totals for dashboards. */
|
|
15
15
|
export default function YearTotalsWidget (props) {
|
|
16
|
-
return <DataLoader emptyGuide={false} content={<YearTotalsPanel {...props} />} />
|
|
16
|
+
return <DataLoader emptyGuide={false} source="projection" content={<YearTotalsPanel {...props} />} />
|
|
17
17
|
}
|