All files / resources/api/v2 radios.ts

0% Statements 0/43
0% Branches 0/1
0% Functions 0/1
0% Lines 0/43

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                                                                                                                       
import {
  LambdaApiFunction,
  handleResourceApi
} from './_base';
 
import { api401Body } from '@/types/api/_shared';
import {
  GetAllRadiosApi, RadioObject
} from '@/types/api/radios';
import {
  TABLE_RADIOS, typedFullScan
} from '@/utils/backend/dynamoTyped';
import { getLogger } from '@/utils/common/logger';
 
const logger = getLogger('radios');
 
const GET: LambdaApiFunction<GetAllRadiosApi> = async function (event, user) {
  logger.trace('GET', ...arguments);
 
  // Authenticate the user
  if (user === null) {
    return [
      401,
      api401Body,
    ];
  }
 
  const radios = await typedFullScan<RadioObject>({
    TableName: TABLE_RADIOS,
    ExpressionAttributeNames: {
      '#InUse': 'InUse',
      '#HasEvents': 'HasEvents',
      '#RadioID': 'RadioID',
      '#Name': 'Name',
      '#Count': 'Count',
      '#EventsCount': 'EventsCount',
    },
    ExpressionAttributeValues: {
      ':InUse': 'Y',
      ':HasEvents': 'Y',
    },
    FilterExpression: '#InUse = :InUse OR #HasEvents = :HasEvents',
    ProjectionExpression: '#RadioID,#Name,#Count,#EventsCount',
  });
 
  return [
    200,
    {
      count: radios.Items.length,
      loadedAll: radios.LastEvaluatedKey === null,
      runs: radios.Runs,
      radios: radios.Items,
    },
  ];
};
 
export const main = handleResourceApi.bind(null, {
  GET,
});