UNPKG

trainingpeaks-sdk

Version:
44 lines (43 loc) 1.6 kB
import { createHttpError, isHttpError } from '../../adapters/errors/http-errors.js'; import { ERROR_CODES, ERROR_MESSAGES } from '../../domain/errors/error-codes.js'; import { getAthleteIdFromSession } from '../../shared/index.js'; const getWorkoutsList = async (command, deps) => { try { const athleteId = command.athleteId || (await getAthleteIdFromSession({ sessionStorage: deps.sessionStorage, logger: deps.logger, })); deps.logger.info('Get workouts list entrypoint called', { athleteId, startDate: command.startDate, endDate: command.endDate, }); const apiResponse = await deps.tpRepository.getWorkoutsList({ athleteId, startDate: command.startDate, endDate: command.endDate, }); return apiResponse; } catch (error) { deps.logger.error('Failed to get workouts list', { error }); if (isHttpError(error)) { throw error; } const httpErrorResponse = { status: 500, statusText: 'Internal Server Error', data: { message: error instanceof Error ? error.message : ERROR_MESSAGES[ERROR_CODES.WORKOUT_LIST_ERROR], }, }; throw createHttpError(httpErrorResponse, { url: 'workouts', method: 'GET', }); } }; export const getWorkoutsListEntrypoint = (dependencies) => (command) => getWorkoutsList(command, dependencies);