add operator

 avatar
unknown
javascript
a month ago
1.6 kB
4
Indexable
async function addOperator(body) {
  const bodyParsed = JSON.parse(body || "{}");
  let { first_name, last_name, email, username, salary, password, totalJobs, jobsThisMonth } = bodyParsed;
  const user = {
    user_role: "operator",
    first_name,
    last_name,
    email,
    username,
    password
  };
  try {
    const addUserToCognito = await createCognitoUser({ username, email, password });
    if (!addUserToCognito) {
      return {
        statusCode: 500,
        body: JSON.stringify({ message: "Could not add user to Cognito", user }),
        headers: headers2
      };
    }
    const userAdded = await createUser(JSON.stringify(user));
    if (!userAdded) {
      return {
        statusCode: 500,
        body: JSON.stringify({ message: "Could not insert user data" }),
        headers: headers2
      };
    }
    const user_id = JSON.parse(userAdded.body).user_id;
    salary = Number(salary);
    totalJobs = Number(totalJobs);
    jobsThisMonth = Number(jobsThisMonth);
    const operatorCommand = new import_lib_dynamodb2.PutCommand({
      TableName: "Operator",
      Item: {
        user_id,
        salary,
        totalJobs,
        jobsThisMonth
      }
    });
    await dynamodb2.send(operatorCommand);
    return {
      statusCode: 200,
      body: JSON.stringify({ message: "Operator data inserted successfully", user_id }),
      headers: headers2
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: "Could not insert operator data " + error }),
      headers: headers2
    };
  }
}
Editor is loading...
Leave a Comment