registerDrone.ts 906 Bytes
import * as R from "ramda";
import { Context } from "../";
import Drone from "entity/Drone";
import Model from "entity/Model";
import User from "entity/User";

export default async (root: {}, { input: { modelId, name } }, context: Context) => {
  const { connection, session } = context;
  const droneRepo = connection.getRepository(Drone);
  const modelRepo = connection.getRepository(Model);
  const userRepo = connection.getRepository(User);

  const userId = session.id;
  const user = await userRepo.findOneById(userId);

  if (R.isNil(user)) throw new Error("Session Error");

  const model = await modelRepo.findOneById(modelId);

  if (R.isNil(model)) throw new Error("Invalid Drone Model");

  let drone = new Drone();
  drone.name = name;
  drone.date = new Date().toString();
  drone.owner = user!;
  drone.model = model;

  await droneRepo.save(drone);
  console.log(drone);
  return drone;
};