@adobe/spacecat-shared-data-access 3.33.1 → 3.34.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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [@adobe/spacecat-shared-data-access-v3.34.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v3.33.1...@adobe/spacecat-shared-data-access-v3.34.0) (2026-03-31)
2
+
3
+ ### Features
4
+
5
+ * adds revoke suggestion grants ([#1465](https://github.com/adobe/spacecat-shared/issues/1465)) ([8081661](https://github.com/adobe/spacecat-shared/commit/80816616d26a9b49b764b15d934881456f7ff656))
6
+
1
7
  ## [@adobe/spacecat-shared-data-access-v3.33.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v3.33.0...@adobe/spacecat-shared-data-access-v3.33.1) (2026-03-31)
2
8
 
3
9
  ### Bug Fixes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-data-access",
3
- "version": "3.33.1",
3
+ "version": "3.34.0",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "engines": {
@@ -17,8 +17,9 @@ import DataAccessError from '../../errors/data-access.error.js';
17
17
 
18
18
  /**
19
19
  * SuggestionGrantCollection - Manages SuggestionGrant records (suggestion_grants table).
20
- * Table is insert-only; inserts happen via grant_suggestions RPC. This collection
21
- * provides read-only lookup by suggestion IDs.
20
+ * Inserts happen via the grant_suggestions RPC and deletes via the revoke_suggestion_grant
21
+ * RPC. This collection provides read-only lookup by suggestion IDs as well as grant and
22
+ * revoke operations.
22
23
  *
23
24
  * @class SuggestionGrantCollection
24
25
  * @extends BaseCollection
@@ -172,6 +173,55 @@ class SuggestionGrantCollection extends BaseCollection {
172
173
 
173
174
  return { success: true, grantedSuggestions: row.granted_suggestions };
174
175
  }
176
+
177
+ /**
178
+ * Invokes the revoke_suggestion_grant RPC. Deletes suggestion_grants rows for the given
179
+ * grant ID and decrements tokens.used by 1.
180
+ * RPC name and parameter shape live in this collection (suggestion_grants).
181
+ *
182
+ * @async
183
+ * @param {string} grantId - Grant ID to revoke.
184
+ * @returns {Promise<{ data: Array|null, error: object|null }>}
185
+ * @throws {DataAccessError} - On missing grantId.
186
+ */
187
+ async invokeRevokeSuggestionGrantRpc(grantId) {
188
+ if (!hasText(grantId)) {
189
+ throw new DataAccessError('invokeRevokeSuggestionGrantRpc: grantId is required', this);
190
+ }
191
+ return this.postgrestService.rpc('revoke_suggestion_grant', {
192
+ p_grant_id: grantId,
193
+ });
194
+ }
195
+
196
+ /**
197
+ * Revokes a suggestion grant by grant ID. Calls the revoke_suggestion_grant RPC to
198
+ * atomically delete suggestion_grants rows and refund the consumed token.
199
+ *
200
+ * @async
201
+ * @param {string} grantId - The grant ID to revoke.
202
+ * @returns {Promise<{ success: boolean, reason?: string, revokedCount?: number }>}
203
+ * @throws {DataAccessError} - On missing inputs or RPC failure.
204
+ */
205
+ async revokeSuggestionGrant(grantId) {
206
+ if (!hasText(grantId)) {
207
+ throw new DataAccessError('revokeSuggestionGrant: grantId is required', this);
208
+ }
209
+
210
+ const rpcResult = await this.invokeRevokeSuggestionGrantRpc(grantId);
211
+ const { data, error } = rpcResult;
212
+
213
+ if (error) {
214
+ this.log.error('revokeSuggestionGrant: RPC failed', error);
215
+ throw new DataAccessError('Failed to revoke suggestion grant (revoke_suggestion_grant)', this, error);
216
+ }
217
+
218
+ const row = Array.isArray(data) && data.length > 0 ? data[0] : null;
219
+ if (!row || !row.success) {
220
+ return { success: false, reason: row?.reason || 'rpc_no_result' };
221
+ }
222
+
223
+ return { success: true, revokedCount: row.revoked_count };
224
+ }
175
225
  }
176
226
 
177
227
  export default SuggestionGrantCollection;
@@ -30,6 +30,7 @@ const schema = new SchemaBuilder(SuggestionGrant, SuggestionGrantCollection)
30
30
  type: 'string', required: false, postgrestIgnore: true,
31
31
  })
32
32
  .addIndex({ composite: ['suggestionId'] }, { composite: [] })
33
+ .addIndex({ composite: ['tokenId'] }, { composite: [] })
33
34
  .addAttribute('suggestionId', {
34
35
  type: 'string',
35
36
  required: true,