All files / utils entitySubscriptions.js

100% Statements 14/14
50% Branches 3/6
100% Functions 5/5
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 775x 5x 5x                           2x               2x     2x                                                 31x             31x 31x     2x     31x 31x   2x           5x  
const { QueryCommand } = require('@aws-sdk/lib-dynamodb')
const { batchDelete } = require('./batchWrite')
const { adjustSubscriberCount } = require('./entitySubscriberCount')
 
/**
 * Deletes every subscription row pointing at an entity that is truly gone
 * (deleted, not merely ownership-transferred — a transferred entity keeps its
 * id, so its subscriptions stay valid). Uses the entityIndex GSI, the same
 * reverse-lookup role EntityLinksTable's entityIndex GSI plays for cleaning
 * up incoming links.
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {string} tableName
 * @param {string} entityId
 */
async function deleteAllSubscriptionsForEntity(docClient, tableName, entityId) {
  const result = await docClient.send(
    new QueryCommand({
      TableName: tableName,
      IndexName: 'entityIndex',
      KeyConditionExpression: 'entityId = :entityId',
      ExpressionAttributeValues: { ':entityId': entityId }
    })
  )
  await batchDelete(
    docClient,
    tableName,
    (result.Items || []).map((s) => ({ userId: s.userId, entityId: s.entityId }))
  )
}
 
/**
 * Deletes every subscription a given user holds (as subscriber) to any
 * entity, regardless of who owns those entities. Used when the user's own
 * account is deleted.
 *
 * Unlike deleteAllSubscriptionsForEntity, the target entities here are NOT
 * being deleted themselves — only this one user's subscription to each is
 * going away — so their subscriber-count rows must be decremented rather
 * than dropped.
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {string} tableName
 * @param {string} userId
 * @param {string} [subscriberCountTableName]
 */
async function deleteAllSubscriptionsForUser(
  docClient,
  tableName,
  userId,
  subscriberCountTableName
) {
  const result = await docClient.send(
    new QueryCommand({
      TableName: tableName,
      KeyConditionExpression: 'userId = :userId',
      ExpressionAttributeValues: { ':userId': userId }
    })
  )
  const subscriptions = result.Items || []
  await batchDelete(
    docClient,
    tableName,
    subscriptions.map((s) => ({ userId: s.userId, entityId: s.entityId }))
  )
 
  Eif (subscriberCountTableName) {
    await Promise.all(
      subscriptions.map((s) =>
        adjustSubscriberCount(docClient, subscriberCountTableName, s.entityId, -1)
      )
    )
  }
}
 
module.exports = { deleteAllSubscriptionsForEntity, deleteAllSubscriptionsForUser }