import { DocumentClient } from "aws-sdk/clients/dynamodb";
import { initCommonMiddlewares } from "../middlewares";
import { IPlatformRecord } from "./contract";

const { PLATFORM_TABLE_NAME } = process.env;

const ddb = new DocumentClient();

async function awsHandler(event: IPlatformRecord): Promise<IPlatformRecord> {
  const { codename, dist, icon, dockerImage, os } = event;

  await ddb
    .put({
      TableName: PLATFORM_TABLE_NAME,
      Item: {
        codename,
        dist,
        icon,
        dockerImage,
        os,
      },
    })
    .promise();

  return {
    codename,
    dist,
    icon,
    dockerImage,
    os,
  };
}

export const handler = initCommonMiddlewares(awsHandler);
