Merge pull request #2018 from overleaf/spd-handle-undefined-in-deletedproject

Handle 'undefined' values when creating DeletedProject objects

GitOrigin-RevId: 1d34d3b44feb50fd5501b1c35f06f62c0170b861
This commit is contained in:
Simon Detheridge 2019-07-26 16:27:28 +01:00 committed by sharelatex
parent 2243f42f9b
commit cf22393dff
2 changed files with 60 additions and 48 deletions

View file

@ -167,31 +167,37 @@ async function deleteProject(project_id, options = {}) {
throw new Errors.NotFoundError('project not found')
}
let deleterData = {
deletedAt: new Date(),
deleterId:
options.deleterUser != null ? options.deleterUser._id : undefined,
deleterIpAddress: options.ipAddress,
deletedProjectId: project._id,
deletedProjectOwnerId: project.owner_ref,
deletedProjectCollaboratorIds: project.collaberator_refs,
deletedProjectReadOnlyIds: project.readOnly_refs,
deletedProjectReadWriteTokenAccessIds:
project.tokenAccessReadAndWrite_refs,
deletedProjectOverleafId: project.overleaf
? project.overleaf.id
: undefined,
deletedProjectOverleafHistoryId:
project.overleaf && project.overleaf.history
? project.overleaf.history.id
: undefined,
deletedProjectReadOnlyTokenAccessIds: project.tokenAccessReadOnly_refs,
deletedProjectReadWriteToken: project.tokens.readAndWrite,
deletedProjectReadOnlyToken: project.tokens.readOnly,
deletedProjectLastUpdatedAt: project.lastUpdated
}
Object.keys(deleterData).forEach(
key => (deleterData[key] === undefined ? delete deleterData[key] : '')
)
await DeletedProject.create({
project: project,
deleterData: {
deletedAt: new Date(),
deleterId:
options.deleterUser != null ? options.deleterUser._id : undefined,
deleterIpAddress: options.ipAddress,
deletedProjectId: project._id,
deletedProjectOwnerId: project.owner_ref,
deletedProjectCollaboratorIds: project.collaberator_refs,
deletedProjectReadOnlyIds: project.readOnly_refs,
deletedProjectReadWriteTokenAccessIds:
project.tokenAccessReadAndWrite_refs,
deletedProjectOverleafId: project.overleaf
? project.overleaf.id
: undefined,
deletedProjectOverleafHistoryId:
project.overleaf && project.overleaf.history
? project.overleaf.history.id
: undefined,
deletedProjectReadOnlyTokenAccessIds: project.tokenAccessReadOnly_refs,
deletedProjectReadWriteToken: project.tokens.readAndWrite,
deletedProjectReadOnlyToken: project.tokens.readOnly,
deletedProjectLastUpdatedAt: project.lastUpdated
}
deleterData: deleterData
})
const flushProjectToMongoAndDelete = promisify(

View file

@ -12,34 +12,40 @@ DeletedProject.find({}, (error, deletedProjects) => {
(deletedProject, cb) => {
if (deletedProject.project) {
const src = deletedProject.project
let values = {
'deleterData.deletedProjectId': src._id,
'deleterData.deletedProjectOwnerId': src.owner_ref,
'deleterData.deletedProjectCollaboratorIds': src.collaberator_refs,
'deleterData.deletedProjectReadOnlyIds': src.readOnly_refs,
'deleterData.deletedProjectReadWriteToken': src.tokens
? src.tokens.readAndWrite
: undefined,
'deleterData.deletedProjectOverleafId': src.overleaf
? src.overleaf.id
: undefined,
'deleterData.deletedProjectOverleafHistoryId':
src.overleaf && src.overleaf.history
? src.overleaf.history.id
: undefined,
'deleterData.deletedProjectReadOnlyToken': src.tokens
? src.tokens.readOnly
: undefined,
'deleterData.deletedProjectReadWriteTokenAccessIds':
src.tokenAccessReadOnly_refs,
'deleterData.deletedProjectReadOnlyTokenAccessIds':
src.tokenAccessReadAndWrite_refs,
'deleterData.deletedProjectLastUpdatedAt': src.lastUpdated
}
Object.keys(values).forEach(
key => (values[key] === undefined ? delete values[key] : '')
)
DeletedProject.findOneAndUpdate(
{ _id: deletedProject._id },
{
$set: {
'deleterData.deletedProjectId': src._id,
'deleterData.deletedProjectOwnerId': src.owner_ref,
'deleterData.deletedProjectCollaboratorIds':
src.collaberator_refs,
'deleterData.deletedProjectReadOnlyIds': src.readOnly_refs,
'deleterData.deletedProjectReadWriteToken': src.tokens
? src.tokens.readAndWrite
: undefined,
'deleterData.deletedProjectOverleafId': src.overleaf
? src.overleaf.id
: undefined,
'deleterData.deletedProjectOverleafHistoryId':
src.overleaf && src.overleaf.history
? src.overleaf.history.id
: undefined,
'deleterData.deletedProjectReadOnlyToken': src.tokens
? src.tokens.readOnly
: undefined,
'deleterData.deletedProjectReadWriteTokenAccessIds':
src.tokenAccessReadOnly_refs,
'deleterData.deletedProjectReadOnlyTokenAccessIds':
src.tokenAccessReadAndWrite_refs,
'deleterData.deletedProjectLastUpdatedAt': src.lastUpdated
}
$set: values
},
cb
)