Skip to content

Commit

Permalink
fix(user): corrige une erreur de syntaxe (qui aurait dû être remontée…
Browse files Browse the repository at this point in the history
… par eslint, clairement)
  • Loading branch information
thom4parisot committed Feb 12, 2025
1 parent df3656e commit b23fa4a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 89 deletions.
1 change: 0 additions & 1 deletion graphql/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,3 @@ articleSchema.post('remove', async function () {
})

module.exports = mongoose.model('Article', articleSchema)
module.exports.schema = articleSchema
1 change: 0 additions & 1 deletion graphql/models/corpus.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ const corpusSchema = new Schema(
)

module.exports = mongoose.model('Corpus', corpusSchema)
module.exports.schema = corpusSchema
58 changes: 30 additions & 28 deletions graphql/models/tag.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
const mongoose = require('mongoose');
const mongoose = require('mongoose')
const toHex = require('colornames')
const Schema = mongoose.Schema;
const Schema = mongoose.Schema

const tagSchema = new Schema({
name: {
type: String,
required: true
},
description: {
type: String
},
owner: {
const tagSchema = new Schema(
{
name: {
type: String,
required: true,
},
description: {
type: String,
},
owner: {
type: Schema.Types.ObjectId,
ref: 'User',
},
color: {
type: String,
get: (color) => {
return toHex((color ?? '').replace('grey', 'gray')) || color || '#ccc'
},
},
// TODO remove this link
articles: [
{
type: Schema.Types.ObjectId,
ref: 'Article',
},
],
},
color: {
type: String,
get: color => {
return toHex((color ?? '').replace('grey', 'gray')) || color || '#ccc'
}
},
// TODO remove this link
articles:[
{
type: Schema.Types.ObjectId,
ref: 'Article'
}
]
}, {timestamps: true});
{ timestamps: true }
)

tagSchema.post('remove', async function () {
await this.model('User').updateOne(
{ _id: this.owner },
{ $pull: { tags: this.id }}
{ $pull: { tags: this.id } }
)

await this.model('Article').updateMany(
Expand All @@ -41,5 +44,4 @@ tagSchema.post('remove', async function () {
)
})

module.exports = mongoose.model('Tag', tagSchema);
module.exports.schema = tagSchema;
module.exports = mongoose.model('Tag', tagSchema)
27 changes: 14 additions & 13 deletions graphql/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,21 @@ userSchema.methods.comparePassword = async function (password) {
return bcrypt.compare(password, oldPassword)
}

userSchema.methods.createDefaultArticle = async function createDefaultArticle () {
const newArticle = await this.model('Article').create({
title: defaultArticle.title,
zoteroLink: defaultArticle.zoteroLink,
owner: this,
workingVersion: {
metadata: defaultArticle.metadata,
bib: defaultArticle.bib,
md: defaultArticle.md
},
})
userSchema.methods.createDefaultArticle =
async function createDefaultArticle() {
const newArticle = await this.model('Article').create({
title: defaultArticle.title,
zoteroLink: defaultArticle.zoteroLink,
owner: this,
workingVersion: {
metadata: defaultArticle.metadata,
bib: defaultArticle.bib,
md: defaultArticle.md,
},
})

await newArticle.createNewVersion({ mode: 'MINOR', user: this })
await newArticle.createNewVersion({ mode: 'MINOR', user: this })
}

userSchema.statics.assessLogin = async function assessLogin(query) {
const user = this.findOne(query)
Expand All @@ -105,4 +107,3 @@ userSchema.virtual('authTypes').get(function () {
})

module.exports = mongoose.model('User', userSchema)
module.exports.schema = userSchema
1 change: 0 additions & 1 deletion graphql/models/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@ const versionSchema = new Schema(
)

module.exports = mongoose.model('Version', versionSchema)
module.exports.schema = versionSchema
108 changes: 63 additions & 45 deletions graphql/models/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,86 @@ const Schema = mongoose.Schema
const WorkspaceMemberSchema = new Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
ref: 'User',
},
role: String
role: String,
})

const workspaceSchema = new Schema({
name: {
type: String,
required: true,
const workspaceSchema = new Schema(
{
name: {
type: String,
required: true,
},
color: {
type: String,
required: true,
get: (color) => toHex(color) || color || '#ccc',
},
description: {
type: String,
},
bibliographyStyle: {
type: String,
},
members: [WorkspaceMemberSchema],
articles: [
{
type: Schema.Types.ObjectId,
ref: 'Article',
},
],
creator: {
type: Schema.Types.ObjectId,
ref: 'User',
},
},
color: {
type: String,
required: true,
get: color => toHex(color) || color || '#ccc'
},
description: {
type: String
},
bibliographyStyle: {
type: String
},
members: [ WorkspaceMemberSchema ],
articles: [{
type: Schema.Types.ObjectId,
ref: 'Article'
}],
creator: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}, { timestamps: true })
{ timestamps: true }
)

workspaceSchema.methods.findMembersByArticle = async function findMembersByArticle (articleId) {
const result = await this.aggregate([
{ $match: { articles: articleId } },
// flatten members in order to create a unique set
{ $unwind: '$members' },
{ $group: { _id: null, memberIds: { $addToSet: '$members' } } },
{ $lookup: { from: 'users', localField: 'memberIds', foreignField: '_id', as: 'members' } }
])
return result[0].members
}
workspaceSchema.methods.findMembersByArticle =
async function findMembersByArticle(articleId) {
const result = await this.aggregate([
{ $match: { articles: articleId } },
// flatten members in order to create a unique set
{ $unwind: '$members' },
{ $group: { _id: null, memberIds: { $addToSet: '$members' } } },
{
$lookup: {
from: 'users',
localField: 'memberIds',
foreignField: '_id',
as: 'members',
},
},
])
return result[0].members
}

workspaceSchema.statics.getWorkspaceById = async function getWorkspaceById(workspaceId, user) {
workspaceSchema.statics.getWorkspaceById = async function getWorkspaceById(
workspaceId,
user
) {
if (user?.admin === true) {
const workspace = await this.findById(workspaceId)
if (!workspace) {
throw new ApiError('NOT_FOUND', `Unable to find workspace with id ${workspaceId}`)
throw new ApiError(
'NOT_FOUND',
`Unable to find workspace with id ${workspaceId}`
)
}
return workspace
}
const workspace = await this.findOne({
$and: [
{ _id: workspaceId },
{ 'members.user': user?._id }
]
$and: [{ _id: workspaceId }, { 'members.user': user?._id }],
})
if (!workspace) {
throw new ApiError('NOT_FOUND', `Unable to find workspace with id ${workspaceId} for user with id ${user?._id}`)
throw new ApiError(
'NOT_FOUND',
`Unable to find workspace with id ${workspaceId} for user with id ${user?._id}`
)
}
return workspace
}

module.exports = mongoose.model('Workspace', workspaceSchema)
module.exports.schema = workspaceSchema

0 comments on commit b23fa4a

Please sign in to comment.