@ossy/resources 3.6.2 → 3.7.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ossy/resources",
3
3
  "description": "Resource domain — aggregate and events for the Ossy resource model",
4
- "version": "3.6.2",
4
+ "version": "3.7.1",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "main": "./src/index.js",
@@ -23,9 +23,9 @@
23
23
  "src": "./src"
24
24
  },
25
25
  "dependencies": {
26
- "@ossy/event-store": "^3.6.2",
26
+ "@ossy/event-store": "^3.7.0",
27
27
  "@ossy/fold": "^3.4.0",
28
- "@ossy/platform": "^3.6.2",
28
+ "@ossy/platform": "^3.7.1",
29
29
  "@ossy/schema": "^3.6.1"
30
30
  },
31
31
  "peerDependencies": {
@@ -49,5 +49,5 @@
49
49
  "/src",
50
50
  "README.md"
51
51
  ],
52
- "gitHead": "d81b840c5421995e8ac7550e5b37ae1145abcba9"
52
+ "gitHead": "1ee220424da6b065034a7f52f52aa4a6ce7c88d5"
53
53
  }
@@ -1,4 +1,4 @@
1
- import { accessFilter } from './resources.queries.js'
1
+ import { accessFilter, ResourcesQueries } from './resources.queries.js'
2
2
 
3
3
  // Helper to build a minimal Policy aggregate document that policyToQueryClause expects.
4
4
  function makePolicy({ effect = 'allow', actions = ['resource:read'], where = {} } = {}) {
@@ -132,3 +132,41 @@ describe('accessFilter', () => {
132
132
  })
133
133
 
134
134
  })
135
+
136
+ describe('ResourcesQueries.buildMongoQuery', () => {
137
+ it('scopes a folder list to belongsTo, location, and status — not document type', () => {
138
+ const query = ResourcesQueries.buildMongoQuery({
139
+ belongsTo: 'ws-1',
140
+ location: '/Marketing/',
141
+ })
142
+ expect(query).toEqual({
143
+ 'state.belongsTo': 'ws-1',
144
+ 'state.location': '/Marketing/',
145
+ 'state.status': { $ne: 'removed' },
146
+ })
147
+ expect(query.type).toBeUndefined()
148
+ })
149
+
150
+ it('does not add a schema-id type regex', () => {
151
+ const query = ResourcesQueries.buildMongoQuery({ belongsTo: 'ws-1', location: '/' })
152
+ expect(JSON.stringify(query)).not.toMatch(/schema\//)
153
+ })
154
+
155
+ it('AND-combines the folder query with accessFilter when a user is passed', () => {
156
+ const user = { id: 'u-1', workspaces: ['ws-1'], policies: [] }
157
+ const query = ResourcesQueries.buildMongoQuery(
158
+ { belongsTo: 'ws-1', location: '/@ossy/jobs/' },
159
+ user,
160
+ )
161
+ expect(query).toEqual({
162
+ $and: [
163
+ {
164
+ 'state.belongsTo': 'ws-1',
165
+ 'state.location': '/@ossy/jobs/',
166
+ 'state.status': { $ne: 'removed' },
167
+ },
168
+ accessFilter(user),
169
+ ],
170
+ })
171
+ })
172
+ })
@@ -1,4 +1,7 @@
1
1
  import { attachResourceMediaUrls } from './resources.attach-media-urls.js'
2
+ import { createLogger } from '@ossy/observability'
3
+
4
+ const log = createLogger('resources')
2
5
 
3
6
  export function mediaContextFromRun({ payload, req }) {
4
7
  return {
@@ -7,12 +10,24 @@ export function mediaContextFromRun({ payload, req }) {
7
10
  }
8
11
  }
9
12
 
13
+ async function attachOrKeep (resource, context) {
14
+ try {
15
+ return await attachResourceMediaUrls(resource, context)
16
+ } catch (error) {
17
+ log.warn('[withResourceMedia] skipping media urls', {
18
+ resourceId: resource?.id,
19
+ error: error?.message,
20
+ })
21
+ return resource
22
+ }
23
+ }
24
+
10
25
  export async function withResourceMedia(result, context) {
11
26
  if (Array.isArray(result)) {
12
- return Promise.all(result.map(r => attachResourceMediaUrls(r, context)))
27
+ return Promise.all(result.map(r => attachOrKeep(r, context)))
13
28
  }
14
29
  if (result && typeof result === 'object' && result.id) {
15
- return attachResourceMediaUrls(result, context)
30
+ return attachOrKeep(result, context)
16
31
  }
17
32
  return result
18
33
  }
@@ -1,6 +1,10 @@
1
1
  import { StorageClient } from '@ossy/platform'
2
2
  import { Aggregate } from '@ossy/event-store'
3
+ import { coerceStorageKey } from '@ossy/platform/storage-keys'
3
4
  import { Workspace } from '@ossy/workspaces/server'
5
+ import { createLogger } from '@ossy/observability'
6
+
7
+ const log = createLogger('resources')
4
8
 
5
9
  /**
6
10
  * Adds `content.src` (and `content.sizes[*]` URLs) for file resources, respecting
@@ -39,11 +43,19 @@ export async function attachResourceMediaUrls(resource, { userId, workspaceId }
39
43
  return { ...resource, content: nextContent }
40
44
  }
41
45
 
42
- if (Key) {
46
+ const originalKey = coerceStorageKey(Key, resource.id)
47
+ if (Key && !originalKey) {
48
+ log.warn('[attachResourceMediaUrls] skipping invalid storage key', {
49
+ resourceId: resource.id,
50
+ key: Key,
51
+ })
52
+ }
53
+
54
+ if (originalKey) {
43
55
  nextContent.src =
44
56
  access === 'public'
45
- ? StorageClient.createDownloadUrl(Key)
46
- : await StorageClient.createPresignedDownloadUrl(Key)
57
+ ? StorageClient.createDownloadUrl(originalKey)
58
+ : await StorageClient.createPresignedDownloadUrl(originalKey)
47
59
  }
48
60
 
49
61
  if (hasSizes) {
@@ -54,10 +66,15 @@ export async function attachResourceMediaUrls(resource, { userId, workspaceId }
54
66
  nextSizes[name] = value
55
67
  continue
56
68
  }
69
+ const sizeFallback = resource.id && /^[A-Za-z0-9_-]+$/.test(name)
70
+ ? `${resource.id}:${name}`
71
+ : resource.id
72
+ const logical = coerceStorageKey(value, sizeFallback)
73
+ if (!logical) continue
57
74
  nextSizes[name] =
58
75
  access === 'public'
59
- ? StorageClient.createDownloadUrl(value)
60
- : await StorageClient.createPresignedDownloadUrl(value)
76
+ ? StorageClient.createDownloadUrl(logical)
77
+ : await StorageClient.createPresignedDownloadUrl(logical)
61
78
  }
62
79
  nextContent.sizes = nextSizes
63
80
  }
@@ -67,18 +67,23 @@ export class ResourcesQueries {
67
67
  return query
68
68
  }
69
69
 
70
+ /**
71
+ * Folder list/search query. Scope is `state.belongsTo` + `state.location`
72
+ * (any schema in that folder). Do not filter document `type` with a schema-id
73
+ * regex — that matches audit snapshots in the same collection and hides
74
+ * leftover `Resource` docs that still live in folders.
75
+ */
76
+ static buildMongoQuery(_query = {}, user = null) {
77
+ const query = ResourcesQueries.#prefixQuery({ ..._query, status: { $ne: 'removed' } })
78
+ return user ? { $and: [query, accessFilter(user)] } : query
79
+ }
80
+
70
81
  static GetResources(_query = {}, user = null) {
71
82
  log.info('[ResourcesQueries][GetResources] Building query')
72
- const query = ResourcesQueries.#prefixQuery({ ..._query, status: { $ne: 'removed' } })
73
- const workspaceScope = query['state.belongsTo'] ?? '(no workspace scope)'
83
+ const mongoQuery = ResourcesQueries.buildMongoQuery(_query, user)
84
+ const workspaceScope = _query.belongsTo ?? '(no workspace scope)'
74
85
  log.info(`[ResourcesQueries][GetResources] Fetching resources for ${workspaceScope}`)
75
86
 
76
- const baseQuery = {
77
- ...query,
78
- type: { $regex: /^@[^/]+\/[^/]+\/schema\// },
79
- }
80
- const mongoQuery = user ? { $and: [baseQuery, accessFilter(user)] } : baseQuery
81
-
82
87
  return Aggregate.Collection.find(mongoQuery, { state: true }).toArray()
83
88
  .then(resources => {
84
89
  log.info(`[ResourcesQueries][GetResources] Found ${resources.length} resources`)