buyDataset.ts 931 Bytes
import * as R from "ramda";
import { Context } from "../";
import Contract from "entity/Contract";
import Drone from "entity/Drone";
import Dataset from "entity/Dataset";
import User from "entity/User";
import Org from "entity/Org";

export default async (root: {}, { id }, context: Context) => {
  const { connection, session } = context;
  console.log("AAA");
  const userRepo = connection.getRepository(User);
  const datasetRepo = connection.getRepository(Dataset);
  const user = await userRepo.findOneById(session.id);

  const datasets = await userRepo
    .createQueryBuilder()
    .relation(User, "datasets")
    .of(user)
    .loadMany();

  const dataset = await datasetRepo.findOneById(id);
  console.log(user);
  user!.datasets = [...datasets, dataset];

  try {
    const result = await userRepo.save(user!);
    console.log("UPDATE", result);
  } catch (err) {
    console.log(err, 11111);
  }

  return dataset;
};