|
1 | 1 | import Project from '../graphQL/resolvers/query';
|
2 | 2 | import { MarketplaceController } from '../interfaces';
|
3 | 3 | import { Projects, Users } from '../models/reactypeModels';
|
| 4 | +import mongoose from 'mongoose'; |
4 | 5 |
|
5 | 6 | // array of objects, objects inside
|
6 | 7 | type Projects = { project: {} }[];
|
@@ -33,36 +34,39 @@ const marketplaceController: MarketplaceController = {
|
33 | 34 | /**
|
34 | 35 | *
|
35 | 36 | * Middleware function that publishes (and saves) a project to the database
|
36 |
| - * @return sends the updated project to the frontend |
| 37 | + * @return sends the updated entire project document to the frontend |
37 | 38 | */
|
38 |
| - publishProject: (req, res, next) => { |
| 39 | + publishProject: async (req, res, next) => { |
39 | 40 | const { _id, project, comments, userId, username, name } = req.body;
|
40 | 41 | const createdAt = Date.now();
|
| 42 | + console.log('Publish Project', _id, project, comments, userId, username, name ) |
| 43 | + |
41 | 44 | if (userId === req.cookies.ssid) {
|
42 |
| - Projects.findOneAndUpdate( |
43 |
| - // looks in projects collection for project by Mongo id |
44 |
| - { _id }, |
45 |
| - // update or insert the project |
46 |
| - { project, createdAt, published: true, comments, name, userId, username }, |
47 |
| - // Options: |
48 |
| - // upsert: true - if none found, inserts new project, otherwise updates it |
49 |
| - // new: true - returns updated document not the original one |
50 |
| - { upsert: true, new: true }, |
51 |
| - (err, result) => { |
52 |
| - if (err) { |
53 |
| - return next({ |
54 |
| - log: `Error in marketplaceController.publishProject: ${err}`, |
55 |
| - message: { |
56 |
| - err: 'Error in marketplaceController.publishProject, check server logs for details' |
57 |
| - } |
58 |
| - }); |
59 |
| - } |
60 |
| - res.locals.publishedProject = result; //returns the entire document |
61 |
| - return next(); |
62 |
| - } |
63 |
| - ); |
| 45 | + |
| 46 | + if (mongoose.isValidObjectId(_id)) { |
| 47 | + const publishedProject = await Projects.findOneAndUpdate |
| 48 | + ( // looks in projects collection for project by Mongo id |
| 49 | + { _id }, |
| 50 | + // update or insert the project |
| 51 | + { project, createdAt, published: true, comments, name, userId, username }, |
| 52 | + // Options: |
| 53 | + // upsert: true - if none found, inserts new project, otherwise updates it |
| 54 | + // new: true - returns updated document not the original one |
| 55 | + { upsert: true, new: true } |
| 56 | + ); |
| 57 | + res.locals.publishedProject = publishedProject; |
| 58 | + return next(); |
| 59 | + }else{ |
| 60 | + const noId = {...project}; |
| 61 | + delete noId._id; //removing the empty string _id from project |
| 62 | + const publishedProject = await Projects.create( { project: noId, createdAt, published: true, comments, name, userId, username }); |
| 63 | + res.locals.publishedProject = publishedProject.toObject({ minimize: false }); |
| 64 | + console.log('published backend new', res.locals.publishedProject) |
| 65 | + return next(); |
| 66 | + } |
64 | 67 | }
|
65 | 68 | else {
|
| 69 | + console.log('userId did not match') |
66 | 70 | // we should not expect a user to be able to access another user's id, but included error handling for unexpected errors
|
67 | 71 | return next({
|
68 | 72 | log: 'Error in marketplaceController.publishProject',
|
|
0 commit comments