<commit_before>import { intersection } from 'lodash';
import { adaptQuery } from 'olymp-collection/server';
import {
  parseURI,
  getImageById,
  getImages,
  getSignedRequest,
  updateImage,
} from './cloudinary';

export default (uri) => {
  const config = parseURI(uri);
  const invalidationTokens = [];
  return {
    name: 'file',
    queries: `
      cloudinaryRequest: CloudinaryRequest
    `,
    mutations: `
      cloudinaryRequestDone(id: String, token: String): File
    `,
    resolvers: {
      queries: {
        file: (source, args, { monk, app }) =>
          monk.collection('item').findOne({ id: args.id }).then((item) => {
            if (item) {
              return item;
            }
            return getImageById(config, args.id).then((image) => {
              monk
                .collection('item')
                .update(
                { id: args.id },
                { ...image, _type: 'file', _appId: app.id },
                { upsert: true }
                )
                .catch(err => console.error(err));
              return image;
            });
          }),
        fileList: (source, { query }, { monk, app }) => {
          const mongoQuery = adaptQuery(query);
          const tags = mongoQuery.tags && mongoQuery.tags.$in;
          console.log(mongoQuery);
          const getFiltered = items =>
            tags // eslint-disable-line
              ? items.filter(item => intersection(tags, item.tags).length > 0)
              : items;

          return getImages(config).then((images) => {
            const filtered = getFiltered(images.filter(x => !x.removed));
            Promise.all(
              filtered.map(item =>
                monk
                  .collection('item')
                  .update(
                  { id: item.id },
                  { ...item, _type: 'file', _appId: app.id },
                  { upsert: true }
                  )
              )
            ).catch(err => console.error(err));
            return filtered;
          });
        },
        cloudinaryRequest: () =><commit_msg>remove console.log<commit_after>import { intersection } from 'lodash';
import { adaptQuery } from 'olymp-collection/server';
import {
  parseURI,
  getImageById,
  getImages,
  getSignedRequest,
  updateImage,
} from './cloudinary';

export default (uri) => {
  const config = parseURI(uri);
  const invalidationTokens = [];
  return {
    name: 'file',
    queries: `
      cloudinaryRequest: CloudinaryRequest
    `,
    mutations: `
      cloudinaryRequestDone(id: String, token: String): File
    `,
    resolvers: {
      queries: {
        file: (source, args, { monk, app }) =>
          monk.collection('item').findOne({ id: args.id }).then((item) => {
            if (item) {
              return item;
            }
            return getImageById(config, args.id).then((image) => {
              monk
                .collection('item')
                .update(
                { id: args.id },
                { ...image, _type: 'file', _appId: app.id },
                { upsert: true }
                )
                .catch(err => console.error(err));
              return image;
            });
          }),
        fileList: (source, { query }, { monk, app }) => {
          const mongoQuery = adaptQuery(query);
          const tags = mongoQuery.tags && mongoQuery.tags.$in;
          const getFiltered = items =>
            tags // eslint-disable-line
              ? items.filter(item => intersection(tags, item.tags).length > 0)
              : items;

          return getImages(config).then((images) => {
            const filtered = getFiltered(images.filter(x => !x.removed));
            Promise.all(
              filtered.map(item =>
                monk
                  .collection('item')
                  .update(
                  { id: item.id },
                  { ...item, _type: 'file', _appId: app.id },
                  { upsert: true }
                  )
              )
            ).catch(err => console.error(err));
            return filtered;
          });
        },
        cloudinaryRequest: () =>
{'fraction_non_alphanumeric': 0.13508941517641374, 'fraction_numerical': 0.0004833252779120348, 'mean_word_length': 1.459298871063577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4538418', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1117, 'n_words': 236}