@open-mercato/core 0.11.1-develop.8572.1.85986dc508 → 0.11.1-develop.8581.1.535dac92b7
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/.turbo/turbo-build.log +1 -1
- package/dist/modules/attachments/di.js +25 -0
- package/dist/modules/attachments/di.js.map +2 -2
- package/dist/modules/dictionaries/api/[dictionaryId]/entries/[entryId]/route.js +1 -1
- package/dist/modules/dictionaries/api/[dictionaryId]/entries/[entryId]/route.js.map +2 -2
- package/dist/modules/dictionaries/api/[dictionaryId]/entries/route.js +2 -1
- package/dist/modules/dictionaries/api/[dictionaryId]/entries/route.js.map +2 -2
- package/dist/modules/dictionaries/api/context.js +11 -8
- package/dist/modules/dictionaries/api/context.js.map +2 -2
- package/dist/modules/dictionaries/api/route.js +2 -1
- package/dist/modules/dictionaries/api/route.js.map +2 -2
- package/dist/modules/dictionaries/commands/entries.js +30 -4
- package/dist/modules/dictionaries/commands/entries.js.map +2 -2
- package/dist/modules/entities/api/relations/options.js +85 -5
- package/dist/modules/entities/api/relations/options.js.map +2 -2
- package/dist/modules/staff/api/interceptors.js +28 -24
- package/dist/modules/staff/api/interceptors.js.map +2 -2
- package/dist/modules/staff/di.js +23 -0
- package/dist/modules/staff/di.js.map +2 -2
- package/package.json +7 -7
- package/src/modules/attachments/di.ts +29 -0
- package/src/modules/dictionaries/api/[dictionaryId]/entries/[entryId]/route.ts +1 -1
- package/src/modules/dictionaries/api/[dictionaryId]/entries/route.ts +2 -1
- package/src/modules/dictionaries/api/context.ts +11 -8
- package/src/modules/dictionaries/api/route.ts +1 -0
- package/src/modules/dictionaries/commands/entries.ts +40 -3
- package/src/modules/entities/api/relations/options.ts +88 -6
- package/src/modules/staff/api/interceptors.ts +33 -26
- package/src/modules/staff/di.ts +25 -0
|
@@ -8,13 +8,39 @@ import {
|
|
|
8
8
|
ensureDictionaryEntryScope,
|
|
9
9
|
registerDictionaryEntryCommands,
|
|
10
10
|
} from '@open-mercato/core/modules/dictionaries/commands/factory'
|
|
11
|
-
import {
|
|
11
|
+
import type { CommandRuntimeContext } from '@open-mercato/shared/lib/commands'
|
|
12
|
+
import { CrudHttpError, forbidden } from '@open-mercato/shared/lib/crud/errors'
|
|
12
13
|
import { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
function normalizeScopeId(value: unknown): string | null {
|
|
16
|
+
if (typeof value !== 'string') return null
|
|
17
|
+
const trimmed = value.trim()
|
|
18
|
+
return trimmed.length > 0 ? trimmed : null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function resolveSelectedOrganizationId(ctx: CommandRuntimeContext): string | null {
|
|
22
|
+
return normalizeScopeId(ctx.selectedOrganizationId ?? ctx.organizationScope?.selectedId ?? null)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolveContextTenantId(ctx: CommandRuntimeContext): string | null {
|
|
26
|
+
return normalizeScopeId(ctx.organizationScope?.tenantId ?? ctx.auth?.tenantId ?? null)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function ensureDictionary(
|
|
30
|
+
em: EntityManager,
|
|
31
|
+
id: string,
|
|
32
|
+
ctx: CommandRuntimeContext,
|
|
33
|
+
): Promise<Dictionary> {
|
|
34
|
+
if (ctx.organizationScope?.selectionRejected) {
|
|
35
|
+
throw forbidden()
|
|
36
|
+
}
|
|
37
|
+
const tenantId = resolveContextTenantId(ctx)
|
|
38
|
+
const organizationId = resolveSelectedOrganizationId(ctx)
|
|
15
39
|
const dictionary = await em.findOne(Dictionary, {
|
|
16
40
|
id,
|
|
17
41
|
deletedAt: null,
|
|
42
|
+
...(tenantId ? { tenantId } : {}),
|
|
43
|
+
...(organizationId ? { organizationId } : {}),
|
|
18
44
|
})
|
|
19
45
|
if (!dictionary) {
|
|
20
46
|
throw new CrudHttpError(404, { error: 'Dictionary not found' })
|
|
@@ -31,16 +57,27 @@ registerDictionaryEntryCommands({
|
|
|
31
57
|
ensureScope: ensureDictionaryEntryScope,
|
|
32
58
|
duplicateError: 'An entry with this value already exists.',
|
|
33
59
|
resolveDictionaryForCreate: async ({ em, ctx, parsed }) => {
|
|
34
|
-
const dictionary = await ensureDictionary(em, parsed.dictionaryId)
|
|
60
|
+
const dictionary = await ensureDictionary(em, parsed.dictionaryId, ctx)
|
|
35
61
|
const scope = { tenantId: dictionary.tenantId, organizationId: dictionary.organizationId }
|
|
36
62
|
ensureDictionaryEntryScope(ctx, scope)
|
|
37
63
|
return { dictionary, scope }
|
|
38
64
|
},
|
|
39
65
|
resolveEntry: async ({ em, ctx, id }) => {
|
|
66
|
+
if (ctx.organizationScope?.selectionRejected) {
|
|
67
|
+
throw forbidden()
|
|
68
|
+
}
|
|
40
69
|
const entry = await findOneWithDecryption(em, DictionaryEntry, id, { populate: ['dictionary'] })
|
|
41
70
|
if (!entry) {
|
|
42
71
|
throw new CrudHttpError(404, { error: 'Dictionary entry not found' })
|
|
43
72
|
}
|
|
73
|
+
const tenantId = resolveContextTenantId(ctx)
|
|
74
|
+
const selectedOrganizationId = resolveSelectedOrganizationId(ctx)
|
|
75
|
+
if (
|
|
76
|
+
(tenantId && entry.tenantId !== tenantId) ||
|
|
77
|
+
(selectedOrganizationId && entry.organizationId !== selectedOrganizationId)
|
|
78
|
+
) {
|
|
79
|
+
throw new CrudHttpError(404, { error: 'Dictionary entry not found' })
|
|
80
|
+
}
|
|
44
81
|
if (entry.dictionary.deletedAt) {
|
|
45
82
|
throw new CrudHttpError(404, { error: 'Dictionary not found' })
|
|
46
83
|
}
|
|
@@ -5,12 +5,14 @@ import { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'
|
|
|
5
5
|
import { CustomEntity } from '@open-mercato/core/modules/entities/data/entities'
|
|
6
6
|
import type { CustomEntitySpec } from '@open-mercato/shared/modules/entities'
|
|
7
7
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
8
|
-
import type
|
|
8
|
+
import { SortDir, type QueryEngine, type QueryOptions } from '@open-mercato/shared/lib/query/types'
|
|
9
9
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
10
|
+
import { relationOptionAccessKey, type RelationOptionAccess } from '@open-mercato/shared/lib/query/relationOptionAccess'
|
|
11
|
+
import { Organization } from '@open-mercato/core/modules/directory/data/entities'
|
|
10
12
|
import { escapeLikePattern } from '@open-mercato/shared/lib/db/escapeLikePattern'
|
|
11
13
|
import { listEntityMetadata } from '@open-mercato/shared/lib/db/entityMetadata'
|
|
12
14
|
import { resolveRegisteredEntityTableName } from '@open-mercato/shared/lib/query/engine'
|
|
13
|
-
import { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
15
|
+
import { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
14
16
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
15
17
|
import { getModules, resolveTranslations } from '@open-mercato/shared/lib/i18n/server'
|
|
16
18
|
import { isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'
|
|
@@ -26,6 +28,13 @@ export const metadata = {
|
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
const ALLOWED_ROUTE_CONTEXT_FIELDS = new Set(['kind', 'entity_id', 'product_id'])
|
|
31
|
+
const RECORD_POLICY_TARGETS = new Map([
|
|
32
|
+
['attachments', 'attachments:attachment'],
|
|
33
|
+
['staff_time_entries', 'staff:staff_time_entry'],
|
|
34
|
+
['staff_time_projects', 'staff:staff_time_project'],
|
|
35
|
+
['staff_time_reports', 'staff:staff_time_report'],
|
|
36
|
+
['staff_time_tasks', 'staff:staff_time_task'],
|
|
37
|
+
])
|
|
29
38
|
const DEFAULT_LABEL_FIELDS = ['name', 'title', 'code', 'email', 'id']
|
|
30
39
|
|
|
31
40
|
const logger = createLogger('entities')
|
|
@@ -85,6 +94,10 @@ export async function GET(req: Request) {
|
|
|
85
94
|
logger.warn('Relation options requested for an unresolvable entity id', { entityId })
|
|
86
95
|
return NextResponse.json({ items: [] })
|
|
87
96
|
}
|
|
97
|
+
const policyTarget = classification.kind === 'system' && registeredTable
|
|
98
|
+
? RECORD_POLICY_TARGETS.get(registeredTable)
|
|
99
|
+
: undefined
|
|
100
|
+
if (policyTarget && policyTarget !== entityId) return forbidden()
|
|
88
101
|
|
|
89
102
|
const rbac = container.resolve('rbacService') as RbacService
|
|
90
103
|
if (classification.kind === 'custom' && !(await rbac.userHasAllFeatures(
|
|
@@ -107,6 +120,11 @@ export async function GET(req: Request) {
|
|
|
107
120
|
throw error
|
|
108
121
|
}
|
|
109
122
|
|
|
123
|
+
const resolveAccess = policyTarget
|
|
124
|
+
? container.resolve<RelationOptionAccess>(relationOptionAccessKey(policyTarget), { allowUnregistered: true })
|
|
125
|
+
: undefined
|
|
126
|
+
if (policyTarget && !resolveAccess) return forbidden()
|
|
127
|
+
|
|
110
128
|
const customEntity = await findOneWithDecryption(em, CustomEntity, {
|
|
111
129
|
entityId,
|
|
112
130
|
isActive: true,
|
|
@@ -150,7 +168,8 @@ export async function GET(req: Request) {
|
|
|
150
168
|
else if (ids.length > 1) filters.id = { $in: ids }
|
|
151
169
|
if (q) filters[labelField] = { $ilike: `%${escapeLikePattern(q)}%` }
|
|
152
170
|
const fields = Array.from(new Set(['id', labelField, ...routeContextFields]))
|
|
153
|
-
const
|
|
171
|
+
const pageSize = Math.min(ids.length || 50, 100)
|
|
172
|
+
const queryOptions: QueryOptions = {
|
|
154
173
|
tenantId: scope.tenantId,
|
|
155
174
|
...(scope.organizationId
|
|
156
175
|
? { organizationId: scope.organizationId }
|
|
@@ -158,9 +177,72 @@ export async function GET(req: Request) {
|
|
|
158
177
|
fields,
|
|
159
178
|
filters,
|
|
160
179
|
...(classification.kind === 'custom' ? { forceCustomEntityStorage: true } : {}),
|
|
161
|
-
page: { page: 1, pageSize
|
|
162
|
-
}
|
|
163
|
-
|
|
180
|
+
page: { page: 1, pageSize },
|
|
181
|
+
}
|
|
182
|
+
let rows: Record<string, unknown>[]
|
|
183
|
+
if (resolveAccess) {
|
|
184
|
+
const tenantId = scope.tenantId
|
|
185
|
+
const organizationIds = scope.organizationId
|
|
186
|
+
? [scope.organizationId]
|
|
187
|
+
: organizationScope.filterIds ?? (await findWithDecryption(
|
|
188
|
+
em, Organization, { tenant: tenantId, deletedAt: null }, { fields: ['id'] }, { tenantId },
|
|
189
|
+
)).map((organization) => organization.id)
|
|
190
|
+
rows = []
|
|
191
|
+
try {
|
|
192
|
+
for (const organizationId of organizationIds) {
|
|
193
|
+
if (requirement && !(await rbac.userHasAllFeatures(auth.sub, requirement.view, { tenantId, organizationId }))) continue
|
|
194
|
+
const access = await resolveAccess({ auth: scopedAuth, container, tenantId, organizationId })
|
|
195
|
+
if (!access) continue
|
|
196
|
+
const scopedOptions = {
|
|
197
|
+
...queryOptions,
|
|
198
|
+
organizationId,
|
|
199
|
+
organizationIds: undefined,
|
|
200
|
+
omitAutomaticTenantOrgScope: true,
|
|
201
|
+
}
|
|
202
|
+
const scopeFilters = { tenant_id: tenantId, organization_id: organizationId }
|
|
203
|
+
const remaining = pageSize - rows.length
|
|
204
|
+
if (access.authorizeIds) {
|
|
205
|
+
const candidateFilters = ids.length ? { id: { $in: ids } } : {}
|
|
206
|
+
let lastId: string | undefined
|
|
207
|
+
while (rows.length < pageSize) {
|
|
208
|
+
const candidates = await qe.query<{ id: string }>(entityId, {
|
|
209
|
+
...scopedOptions,
|
|
210
|
+
fields: ['id'],
|
|
211
|
+
filters: { $and: [scopeFilters, candidateFilters, ...(lastId ? [{ id: { $gt: lastId } }] : [])] },
|
|
212
|
+
sort: [{ field: 'id', dir: SortDir.Asc }],
|
|
213
|
+
page: { page: 1, pageSize: 100 },
|
|
214
|
+
})
|
|
215
|
+
if (candidates.items.length === 0) break
|
|
216
|
+
const authorizedIds = await access.authorizeIds(candidates.items.map(({ id }) => id))
|
|
217
|
+
if (authorizedIds.length) {
|
|
218
|
+
const result = await qe.query<Record<string, unknown>>(entityId, {
|
|
219
|
+
...scopedOptions,
|
|
220
|
+
filters: { $and: [scopeFilters, filters, { id: { $in: authorizedIds } }] },
|
|
221
|
+
page: { page: 1, pageSize: pageSize - rows.length },
|
|
222
|
+
})
|
|
223
|
+
rows.push(...result.items)
|
|
224
|
+
}
|
|
225
|
+
if (candidates.items.length < 100) break
|
|
226
|
+
lastId = candidates.items.at(-1)?.id
|
|
227
|
+
}
|
|
228
|
+
} else if (access.filters) {
|
|
229
|
+
const result = await qe.query<Record<string, unknown>>(entityId, {
|
|
230
|
+
...scopedOptions,
|
|
231
|
+
filters: { $and: [scopeFilters, filters, access.filters] },
|
|
232
|
+
page: { page: 1, pageSize: remaining },
|
|
233
|
+
})
|
|
234
|
+
rows.push(...result.items)
|
|
235
|
+
}
|
|
236
|
+
if (rows.length >= pageSize) break
|
|
237
|
+
}
|
|
238
|
+
} catch (error) {
|
|
239
|
+
if (isCrudHttpError(error)) return NextResponse.json(error.body, { status: error.status })
|
|
240
|
+
throw error
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
rows = (await qe.query<Record<string, unknown>>(entityId, queryOptions)).items ?? []
|
|
244
|
+
}
|
|
245
|
+
const items = rows.map((it) => {
|
|
164
246
|
const routeContext = routeContextFields.reduce<Record<string, unknown>>((acc, field) => {
|
|
165
247
|
if (it[field] !== undefined) {
|
|
166
248
|
acc[field] = it[field]
|
|
@@ -39,6 +39,38 @@ async function hasManageAllFeature(context: InterceptorContext): Promise<boolean
|
|
|
39
39
|
)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
export async function scopeTimeEntryList(
|
|
43
|
+
query: Record<string, unknown> | undefined,
|
|
44
|
+
context: InterceptorContext,
|
|
45
|
+
) {
|
|
46
|
+
if (await hasManageAllFeature(context)) {
|
|
47
|
+
return { ok: true }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const staffMember = await getStaffMemberByUserId(
|
|
51
|
+
context.em,
|
|
52
|
+
context.userId,
|
|
53
|
+
context.tenantId ?? null,
|
|
54
|
+
context.organizationId ?? null,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
if (!staffMember) {
|
|
58
|
+
return {
|
|
59
|
+
ok: false,
|
|
60
|
+
statusCode: 403,
|
|
61
|
+
message: 'User is not a staff member.',
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
ok: true,
|
|
67
|
+
query: {
|
|
68
|
+
...(query ?? {}),
|
|
69
|
+
staffMemberId: staffMember.id,
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
42
74
|
export const interceptors: ApiInterceptor[] = [
|
|
43
75
|
{
|
|
44
76
|
id: 'staff.timesheets.self-scope-widget-data',
|
|
@@ -93,32 +125,7 @@ export const interceptors: ApiInterceptor[] = [
|
|
|
93
125
|
methods: ['GET'],
|
|
94
126
|
priority: 70,
|
|
95
127
|
async before(request, context) {
|
|
96
|
-
|
|
97
|
-
return { ok: true }
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const staffMember = await getStaffMemberByUserId(
|
|
101
|
-
context.em,
|
|
102
|
-
context.userId,
|
|
103
|
-
context.tenantId ?? null,
|
|
104
|
-
context.organizationId ?? null,
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
if (!staffMember) {
|
|
108
|
-
return {
|
|
109
|
-
ok: false,
|
|
110
|
-
statusCode: 403,
|
|
111
|
-
message: 'User is not a staff member.',
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
ok: true,
|
|
117
|
-
query: {
|
|
118
|
-
...(request.query ?? {}),
|
|
119
|
-
staffMemberId: staffMember.id,
|
|
120
|
-
},
|
|
121
|
-
}
|
|
128
|
+
return scopeTimeEntryList(request.query, context)
|
|
122
129
|
},
|
|
123
130
|
},
|
|
124
131
|
]
|
package/src/modules/staff/di.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { defineProcessDiRegistrar } from '@open-mercato/shared/lib/di/process-registrar'
|
|
2
|
+
import { relationOptionAccessKey, type RelationOptionAccess } from '@open-mercato/shared/lib/query/relationOptionAccess'
|
|
3
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
4
|
import { asValue } from 'awilix'
|
|
3
5
|
import type { AppContainer } from '@open-mercato/shared/lib/di/container'
|
|
4
6
|
import {
|
|
@@ -98,6 +100,28 @@ export type ProjectCodeResolver = {
|
|
|
98
100
|
generate(name: string, taken: Set<string>, ctx?: ProjectCodeContext | null): string
|
|
99
101
|
}
|
|
100
102
|
|
|
103
|
+
const timeEntryRelationAccess: RelationOptionAccess = async ({ auth, container, tenantId, organizationId }) => {
|
|
104
|
+
const { scopeTimeEntryList } = await import('./api/interceptors')
|
|
105
|
+
const scoped = await scopeTimeEntryList(undefined, {
|
|
106
|
+
userId: auth.sub,
|
|
107
|
+
tenantId,
|
|
108
|
+
organizationId,
|
|
109
|
+
container,
|
|
110
|
+
em: container.resolve<EntityManager>('em'),
|
|
111
|
+
})
|
|
112
|
+
if (!scoped.ok) return null
|
|
113
|
+
const { buildScopedTimeEntryListFilters } = await import('./api/timesheets/time-entries/route')
|
|
114
|
+
return {
|
|
115
|
+
filters: await buildScopedTimeEntryListFilters({ page: 1, pageSize: 50, staffMemberId: scoped.query?.staffMemberId }, {
|
|
116
|
+
auth: { ...auth, tenantId, orgId: organizationId },
|
|
117
|
+
container,
|
|
118
|
+
organizationScope: null,
|
|
119
|
+
selectedOrganizationId: organizationId,
|
|
120
|
+
organizationIds: [organizationId],
|
|
121
|
+
}),
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
101
125
|
export function register(container: AppContainer) {
|
|
102
126
|
const resolver: AvailabilityAccessResolver = { resolveAvailabilityWriteAccess }
|
|
103
127
|
const timeTrackingResolver: TimeTrackingAccessResolver = { resolveProjectAccess }
|
|
@@ -116,6 +140,7 @@ export function register(container: AppContainer) {
|
|
|
116
140
|
generate: (name, taken, ctx) => deriveProjectCode(name, taken, ctx ?? undefined),
|
|
117
141
|
}
|
|
118
142
|
container.register({
|
|
143
|
+
[relationOptionAccessKey('staff:staff_time_entry')]: asValue(timeEntryRelationAccess),
|
|
119
144
|
availabilityAccessResolver: asValue(resolver),
|
|
120
145
|
timeTrackingAccessResolver: asValue(timeTrackingResolver),
|
|
121
146
|
timeRoundingResolver: asValue(timeRoundingResolver),
|