Last active
August 5, 2022 13:20
-
-
Save ziedHamdi/7d2429a62c6accd0ab72c5c7868bad23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const complaintUpdateById = updateById.wrapResolve(next => async rp => { | |
const {user} = rp.context | |
const {_id, record} = rp.args; | |
let state = record['state']; | |
// allows us to save draft and then only throw auth error | |
let throwAuthError = false | |
if (isPublished(state) && (!user || !user.id) ) { | |
//do not save as published if error will be thrown | |
state = 'DRAFT' | |
throwAuthError = true | |
} | |
//update state to DRAFT if needed | |
record['state'] = state | |
const complaint = await Complaint.findById(_id) | |
if (user?.id != null && !complaint.user) { | |
record['user'] = { | |
userId: user.id, | |
userName: user.name, | |
roleInShop: "USER" | |
} | |
} else if (user != null && complaint.user.userId != user.id && !isSuperAdmin(user)) { | |
throw new AuthorizationError('User not allowed to update complaint', AuthorizationError.errors.ROLE_NOT_ATTRIBUTED) | |
} | |
let {entity, entityGroup} = await completeRpWithEntityData(rp) | |
if (record['lng'] != null) { | |
record.location = {coordinates: [record['lng'], record['lat']]} | |
record.location.type = 'Point' | |
} else if (entity != null) { | |
record.location = entity.location | |
record.location.type = 'Point' | |
} | |
const description = record['desc']; | |
if (description != null) { | |
// record['desc'] = sanitizeHtml(record['desc']) | |
const html = ejsHtmlSerializer.parse(JSON.parse( description ) ); | |
record['innerHtml'] = html.join(" ") | |
} | |
if (!complaint.public && state === "OPEN") { | |
const order = await findOrderForNewComplaint(complaint.entityGroupId) | |
if (order != null) { | |
console.log("order found for complaint : ", complaint.title) | |
//fixme : the complaint can remain not public only for 10 days if the customer has an active subscription | |
record['public'] = false | |
record['orderId'] = (order == null) ? null : order._id | |
} else { | |
record['public'] = true | |
} | |
} | |
const removedAttachments = record.removedAttachments; | |
if (removedAttachments && removedAttachments.length > 0) { | |
for (let i = 0; i < removedAttachments.length; i++) { | |
const attachmentId = removedAttachments[i].attachmentId; | |
if (attachmentId != null) { | |
await ComplaintAttachment.deleteOne({_id: attachmentId}) | |
} else { | |
console.log("no Id for attachment to remove: ", removedAttachments[i]) | |
} | |
} | |
record.removedAttachments = [] | |
} | |
const resolverResult = await next(rp); | |
if( complaint?.user != null && state === 'OPEN' && complaint.state !== 'OPEN' ) { | |
const complaintUpdated = resolverResult.record | |
//register the author of the complaint as a testifier | |
let { | |
currentAction, | |
recap, | |
complaint: complaintStatsUpdated | |
} = await actionHandler.addAction(user, complaintUpdated, ActionTypes.SHOP_ACTION_EXPECTED.name, null) | |
if (complaintUpdated.userActionRecap == null) { | |
complaintUpdated.userActionRecap = {} | |
} | |
complaintUpdated.userActionRecap[ActionTypes.SAME_ISSUE] = recap | |
} | |
//OPTIMIZE entity and entityGroup are loaded from db twice | |
// if (state === "OPEN" || state === "CONFLICT" || state === "REFUSED") { | |
// entity = await updateEntityStats(rp.args.record['entityId'], rp.args.record['category']); | |
// entityGroup = await updateGroupIssuesCount(entity); | |
// if (entity != null) { | |
// rp.args.record['entityGroupId'] = entity.entityGroupId | |
// } | |
// } | |
if(throwAuthError) | |
throw new AuthenticationError("No user logged") | |
return resolverResult | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment