{"size": 1078, "ext": "js", "max_stars_repo_path": "apps/condo/domains/ticket/access/ExportTicketsService.js", "max_stars_repo_name": "tcmike/condo", "max_stars_repo_stars_event_min_datetime": "2021-12-13T16:30:21.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-05T11:05:22.000Z", "max_issues_repo_path": "apps/condo/domains/ticket/access/ExportTicketsService.js", "max_issues_repo_name": "tcmike/condo", "max_issues_repo_issues_event_min_datetime": "2021-12-10T21:47:39.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-21T21:57:26.000Z", "max_forks_repo_path": "apps/condo/domains/ticket/access/ExportTicketsService.js", "max_forks_repo_name": "tcmike/condo", "max_forks_repo_forks_event_min_datetime": "2022-02-04T21:49:16.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-18T18:39:17.000Z"}
{"max_stars_count": 14, "max_issues_count": 3, "max_forks_count": 2, "avg_line_length": 43.12, "max_line_length": 133, "alphanum_fraction": 0.7486085343}
const { throwAuthenticationError } = require('@condo/domains/common/utils/apolloErrorFormatter')
const { checkOrganizationPermission, checkRelatedOrganizationPermission } = require('@condo/domains/organization/utils/accessSchema')
const get = require('lodash/get')
const { find } = require('@core/keystone/schema')

async function canExportTicketsToExcel ({ args: { data: { where } }, authentication: { item: user } }) {
    if (!user) return throwAuthenticationError()
    if (user.deletedAt) return false
    
    if (user.isAdmin) return true
    const organizationId = get(where, ['organization', 'id'])
    if (organizationId) return await checkOrganizationPermission(user.id, organizationId, 'canManageTickets')
    const organizationWhere = get(where, 'organization')
    if (!organizationWhere) return false
    const [organization] = await find('Organization', organizationWhere)
    if (!organization) return false

    return await checkRelatedOrganizationPermission(user.id, organization.id, 'canManageTickets')

}

module.exports = {
    canExportTicketsToExcel,
}
