import { AuthedFetchType } from "../../api/fake-fetch"; import { Representatives } from "../models"; import { errorMessage } from "../../utils"; import { MISSING_ACCOUNT_ID_ERROR_MESSAGE, MISSING_REPRESENTATIVE_ERROR, MISSING_REPRESENTATIVE_ID_ERROR, MISSING_REPRESENTATIVE_NAME_ERROR } from "../../constants"; export default function representatives(authedFetch: AuthedFetchType): Representatives { return { get: ({ accountID, representativeID }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!representativeID) { return errorMessage(MISSING_REPRESENTATIVE_ID_ERROR); } return authedFetch(`/accounts/${accountID}/representatives/${representativeID}`, { method: "GET" }); }, list: ({ accountID }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/representatives`, { method: "GET" }); }, create: ({ accountID, representative }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!representative) { return errorMessage(MISSING_REPRESENTATIVE_ERROR); } if (!representative.name.firstName || !representative.name.lastName) { return errorMessage(MISSING_REPRESENTATIVE_NAME_ERROR); } return authedFetch(`/accounts/${accountID}/representatives`, { method: "POST", body: representative }); }, update: ({ accountID, representative }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!representative) { return errorMessage(MISSING_REPRESENTATIVE_ERROR); } if (!representative.representativeID) { return errorMessage(MISSING_REPRESENTATIVE_ID_ERROR); } if (!representative.name.firstName || !representative.name.lastName) { return errorMessage(MISSING_REPRESENTATIVE_NAME_ERROR); } return authedFetch(`/accounts/${accountID}/representatives/${representative.representativeID}`, { method: "PUT", body: representative }); }, delete: ({ accountID, representativeID }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!representativeID) { return errorMessage(MISSING_REPRESENTATIVE_ID_ERROR); } return authedFetch(`/accounts/${accountID}/representatives/${representativeID}`, { method: "DELETE" }); } } }