31 lines
958 B
TypeScript
31 lines
958 B
TypeScript
import GenericError from "../../../Errors/GenericError";
|
|
import { opportunityCw } from "./opportunities";
|
|
import { CWOpportunity } from "./opportunity.types";
|
|
|
|
/**
|
|
* Fetch a single opportunity by its ConnectWise ID.
|
|
*
|
|
* @param cwOpportunityId - The ConnectWise opportunity ID
|
|
* @returns The full CW opportunity object
|
|
* @throws GenericError if the fetch fails
|
|
*/
|
|
export const fetchOpportunity = async (
|
|
cwOpportunityId: number,
|
|
): Promise<CWOpportunity> => {
|
|
try {
|
|
return await opportunityCw.fetch(cwOpportunityId);
|
|
} catch (error) {
|
|
const errBody = (error as any).response?.data || error;
|
|
console.error(
|
|
`Error fetching opportunity with ID ${cwOpportunityId}:`,
|
|
errBody,
|
|
);
|
|
throw new GenericError({
|
|
name: "FetchOpportunityError",
|
|
message: `Failed to fetch opportunity ${cwOpportunityId}`,
|
|
cause: typeof errBody === "string" ? errBody : JSON.stringify(errBody),
|
|
status: 502,
|
|
});
|
|
}
|
|
};
|