./shared/functions/general-service.ts

mail@pastecode.io avatar
unknown
typescript
a year ago
1.2 kB
5
Indexable
Never
import { Request } from 'lambda-api';
// import { Endpoint } from 'aws-sdk';
import lambda from '../libs/lambda';
import config from '../libs/config';
import { setAuth } from '../libs/auth';
import { ApiModel } from '../models/api.model';
import code from '../libs/code';

export async function getReligion(
  req: Request,
  religionId: number,
): Promise<any> {
  try {
    // lambda.endpoint = new Endpoint('http://localhost:3026');
    const result = await lambda
      .invoke({
        FunctionName: `general-service-${config.stage}-api-v1`,
        InvocationType: 'RequestResponse',
        LogType: 'Tail',
        Payload: JSON.stringify({
          ...setAuth(req),
          httpMethod: 'GET',
          path: `/general/v1/religion-detail/${religionId}`,
        }),
      })
      .promise();

    const payload = JSON.parse(result.Payload as string);
    const body: ApiModel<any> = JSON.parse(payload.body);

    if (body.status) {
      return body.data;
    }

    throw Error(body.message);
  } catch (e) {
    console.error('[ERROR-INVOKE] - func: getReligion', e);
    throw Error(code.integration_error);
  }
}