All files / server/articles editArticle.ts

0% Statements 0/22
0% Branches 0/2
100% Functions 0/0
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                                       
import { IArticle } from '../../shared';
import { IArticleEdit } from '../../shared/types/article';
import upload from '../api/s3';
import { articleModel } from '../model/article';
import { getCategory } from '../model/category';
import { commentModel } from '../model/comment';
import { slugify } from '../utils';
 
export default async (editedArticle: IArticleEdit): Promise<IArticle> => {
  const category = editedArticle.category
    ? await getCategory(editedArticle.category)
    : undefined;
  const slug = slugify(editedArticle.title);
 
  let uploaded;
  if (editedArticle.image) {
    const { createReadStream } = await editedArticle.image;
    const stream = createReadStream();
    uploaded = await upload(stream);
  }
 
  const article = await articleModel.findByIdAndUpdate(
    editedArticle._id,
    {
      ...editedArticle,
      category,
      slug,
      ...(uploaded && { image: uploaded.Location }),
    },
    { new: true },
  );
 
  await commentModel.updateMany({ 'article._id': article._id }, { article });
  return article;
};