Skip to content

Commit

Permalink
fix(codegen): INFRA-316 remove some redundant code, move generate.con…
Browse files Browse the repository at this point in the history
…do.server.utils to codegen
  • Loading branch information
nomerdvadcatpyat committed Jun 13, 2024
1 parent 1c4f943 commit 01ff192
Show file tree
Hide file tree
Showing 37 changed files with 103 additions and 905 deletions.
2 changes: 2 additions & 0 deletions packages/codegen/generate-condo-schema.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//TODO(DOMA-9259): use this generateCondoSchema in all existed miniapps

const fs = require('fs')

const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const { GQLError } = require('@open-condo/keystone/errors')
const { makeRemoteExecutor } = require('@open-condo/keystone/stitchSchema')
const { extractReqLocale } = require('@open-condo/locales/extractReqLocale')

const { DEFAULT_LOCALE } = require('@{{name}}/domains/common/constants')
const { CONDO_ACCESS_TOKEN_KEY, ACCEPT_LANGUAGE, CONDO_REFRESH_TOKEN_KEY } = require('@{{name}}/domains/condo/constants')


const IS_DEBUG = conf.NODE_ENV === 'development' || conf.NODE_ENV === 'test'

const CONDO_API_URL = `${conf.CONDO_DOMAIN}/admin/api`
const DEFAULT_LOCALE = conf.DEFAULT_LOCALE
const CONDO_ACCESS_TOKEN_KEY = conf.CONDO_ACCESS_TOKEN_KEY
const ACCEPT_LANGUAGE = conf.ACCEPT_LANGUAGE
const CONDO_REFRESH_TOKEN_KEY = conf.CONDO_REFRESH_TOKEN_KEY

const client = makeRemoteExecutor(CONDO_API_URL, CONDO_ACCESS_TOKEN_KEY)

Expand Down
5 changes: 5 additions & 0 deletions packages/codegen/templates/app00/bin/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const main = async ([B2BAppName = '{{name}}']) => {
const appName = '{{name}}'
const oidcConf = await prepareCondoAppOidcConfig(appName)
await updateAppEnvFile(appName, 'OIDC_CONDO_CLIENT_CONFIG', JSON.stringify(oidcConf))
await updateAppEnvFile(appName, 'APP_TOKEN_KEY', 'appToken')
await updateAppEnvFile(appName, 'CONDO_ACCESS_TOKEN_KEY', 'condoAccessToken')
await updateAppEnvFile(appName, 'CONDO_REFRESH_TOKEN_KEY', 'condoRefreshToken')
await updateAppEnvFile(appName, 'CONDO_ORGANIZATION_ID_KEY', 'condoOrganizationId')
await updateAppEnvFile(appName, 'ACCEPT_LANGUAGE', 'accept-language')
// 2) prepare B2BApp in condo
await prepareCondoAppB2BAppConfig(appName, B2BAppName)
// 3) migrate initial data and maketypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ const LOCALES = [
RU_LOCALE,
]

const MAX_CONTENT_WIDTH = 1600

module.exports = {
DEFAULT_LOCALE,
LOCALES,
EN_LOCALE,
RU_LOCALE,
MAX_CONTENT_WIDTH
}
15 changes: 0 additions & 15 deletions packages/codegen/templates/app00/domains/condo/gql/fields.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
const COMMON_FIELDS = 'id dv sender { dv fingerprint } v deletedAt newId createdBy { id name } updatedBy { id name } createdAt updatedAt'

const ADDRESS_META_DATA_SUBFIELDS_QUERY_LIST = 'postal_code country country_iso_code federal_district region_fias_id region_kladr_id region_iso_code region_with_type region_type region_type_full region area_fias_id area_kladr_id area_with_type area_type area_type_full area city_fias_id city_kladr_id city_with_type city_type city_type_full city city_area city_district_fias_id city_district_kladr_id city_district_with_type city_district_type city_district_type_full city_district settlement_fias_id settlement_kladr_id settlement_with_type settlement_type settlement_type_full settlement street_fias_id street_kladr_id street_with_type street_type street_type_full street house_fias_id house_kladr_id house_type house_type_full house block_type block_type_full block entrance floor flat_fias_id flat_type flat_type_full flat flat_area square_meter_price flat_price postal_box fias_id fias_code fias_level fias_actuality_state kladr_id geoname_id capital_marker okato oktmo tax_office tax_office_legal timezone geo_lat geo_lon beltway_hit beltway_distance metro { name line distance } qc_geo qc_complete qc_house history_values unparsed_parts source qc'
const ADDRESS_META_SUBFIELDS_QUERY_LIST = `dv value unrestricted_value data { ${ADDRESS_META_DATA_SUBFIELDS_QUERY_LIST} }`

const PROPERTY_MAP_SECTION_FIELDS = 'id type index name preview floors { id type index name units { id type unitType name label preview } }'
const PROPERTY_MAP_JSON_FIELDS = `dv type sections { ${PROPERTY_MAP_SECTION_FIELDS} } parking { ${PROPERTY_MAP_SECTION_FIELDS} }`
const PROPERTY_FIELDS = `id name deletedAt address addressMeta { ${ADDRESS_META_SUBFIELDS_QUERY_LIST} } type map { ${PROPERTY_MAP_JSON_FIELDS} } addressKey`

const CONTACT_FIELDS = `organization { id name } property { id address addressMeta { ${ADDRESS_META_SUBFIELDS_QUERY_LIST} } } name phone unitName unitType email role { id name } isVerified ${COMMON_FIELDS}`


module.exports = {
COMMON_FIELDS,
ADDRESS_META_DATA_SUBFIELDS_QUERY_LIST,
ADDRESS_META_SUBFIELDS_QUERY_LIST,
PROPERTY_MAP_SECTION_FIELDS,
PROPERTY_MAP_JSON_FIELDS,
PROPERTY_FIELDS,
CONTACT_FIELDS,
}
28 changes: 0 additions & 28 deletions packages/codegen/templates/app00/domains/condo/gql/gql.bot.js

This file was deleted.

24 changes: 0 additions & 24 deletions packages/codegen/templates/app00/domains/condo/gql/gql.client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { generateGqlQueries } = require('@open-condo/codegen/generate.gql')

const { PROPERTY_FIELDS: DEFAULT_PROPERTY_FIELDS, CONTACT_FIELDS: DEFAULT_CONTACT_FIELDS } = require('./fields')


const CONDO_PREFIX = 'condo'

Expand All @@ -13,47 +11,25 @@ const B2BAppContext = generateGqlQueries('B2BAppContext', B2B_APP_CONTEXT_FIELDS
const B2B_APP_ROLE_FIELDS = `{ permissions role { id canManageRoles } app { id } ${COMMON_FIELDS} }`
const B2BAppRole = generateGqlQueries('B2BAppRole', B2B_APP_ROLE_FIELDS, CONDO_PREFIX)

const CONTACT_FIELDS = `{ ${DEFAULT_CONTACT_FIELDS} }`
const Contact = generateGqlQueries('Contact', CONTACT_FIELDS, CONDO_PREFIX)

const ORGANIZATION_FIELDS = `{ ${COMMON_FIELDS} }`
const Organization = generateGqlQueries('Organization', ORGANIZATION_FIELDS, CONDO_PREFIX)

const ORGANIZATION_EMPLOYEE_FIELDS = `{ user { id } role { id canManageRoles } ${COMMON_FIELDS} }`
const OrganizationEmployee = generateGqlQueries('OrganizationEmployee', ORGANIZATION_EMPLOYEE_FIELDS, CONDO_PREFIX)

const ORGANIZATION_EMPLOYEE_ROLE_FIELDS = '{ organization { id } name canManageRoles id }'
const OrganizationEmployeeRole = generateGqlQueries('OrganizationEmployeeRole', ORGANIZATION_EMPLOYEE_ROLE_FIELDS, CONDO_PREFIX)

const PROPERTY_FIELDS = `{ ${DEFAULT_PROPERTY_FIELDS} }`
const Property = generateGqlQueries('Property', PROPERTY_FIELDS, CONDO_PREFIX)

const TICKET_FIELDS = `{ source { id name } organization { id name } property { id address } unitType unitName clientName clientPhone details source { id name type } classifier { id place { id name } category { id name } problem { id name } } ${COMMON_FIELDS} }`
const Ticket = generateGqlQueries('Ticket', TICKET_FIELDS, CONDO_PREFIX)

const RESIDENT_FIELDS = `{ user { id name phone deletedAt } ${COMMON_FIELDS} }`

const CONDO_CONFIG = [
{ name: 'B2BAppContext', operations: ['read'], fields: B2B_APP_CONTEXT_FIELDS },
{ name: 'B2BAppRole', operations: ['read', 'create'], fields: B2B_APP_ROLE_FIELDS },
{ name: 'Contact', operations: ['read'], fields: CONTACT_FIELDS },
{ name: 'Organization', operations: ['read'], fields: ORGANIZATION_FIELDS },
{ name: 'OrganizationEmployee', operations: ['read'], fields: ORGANIZATION_EMPLOYEE_FIELDS },
{ name: 'OrganizationEmployeeRole', operations: ['read'], fields: ORGANIZATION_EMPLOYEE_ROLE_FIELDS },
{ name: 'Property', operations: ['read'], fields: PROPERTY_FIELDS },
{ name: 'Ticket', operations: ['read', 'create'], fields: TICKET_FIELDS },
{ name: 'Resident', operations: ['read'], fields: RESIDENT_FIELDS },
]


module.exports = {
CONDO_CONFIG,
B2BAppContext,
B2BAppRole,
Contact,
Organization,
OrganizationEmployee,
OrganizationEmployeeRole,
Property,
Ticket,
}
32 changes: 0 additions & 32 deletions packages/codegen/templates/app00/domains/condo/gql/gql.server.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
const { gql } = require('graphql-tag')

const { generateGqlQueries } = require('@open-condo/codegen/generate.gql')

const {
PROPERTY_FIELDS: DEFAULT_PROPERTY_FIELDS,
ADDRESS_META_SUBFIELDS_QUERY_LIST,
} = require('./fields')


const B2B_APP_ROLE_FIELDS = '{ id permissions role { id } app { id } }'
const B2BAppRole = generateGqlQueries('B2BAppRole', B2B_APP_ROLE_FIELDS)

const CONTACT_FIELDS = '{ id organization { id } name phone email isVerified unitName unitType property { id } }'
const Contact = generateGqlQueries('Contact', CONTACT_FIELDS)

const ORGANIZATION_FIELDS = '{ id }'
const Organization = generateGqlQueries('Organization', ORGANIZATION_FIELDS)

const ORGANIZATION_EMPLOYEE_FIELDS = '{ id role { id } user { id } }'
const OrganizationEmployee = generateGqlQueries('OrganizationEmployee', ORGANIZATION_EMPLOYEE_FIELDS)

const PROPERTY_FIELDS = `{ ${DEFAULT_PROPERTY_FIELDS} }`
const Property = generateGqlQueries('Property', PROPERTY_FIELDS)

const RESIDENT_FIELDS = '{ id user { id name phone } property { id } unitName unitType residentOrganization { id } }'
const Resident = generateGqlQueries('Resident', RESIDENT_FIELDS)

const USER_FIELDS = '{ id phone name email }'
const User = generateGqlQueries('User', USER_FIELDS)

const GET_ALL_PROPERTY_WITH_META_QUERY = gql`
query getAllPropertyIdsWithOrganizationId($where: PropertyWhereInput) {
objs: allProperties(where: $where) { id name organization { id } type address addressMeta { ${ADDRESS_META_SUBFIELDS_QUERY_LIST} } }
}
`

const SEND_B2C_APP_PUSH_MESSAGE_MUTATION = gql`
mutation sendB2CAppPushMessage ($data: SendB2CAppPushMessageInput!) {
result: sendB2CAppPushMessage(data: $data) { id status }
}
`

module.exports = {
B2BAppRole,
Contact,
Organization,
OrganizationEmployee,
Property,
Resident,
User,
GET_ALL_PROPERTY_WITH_META_QUERY,
SEND_B2C_APP_PUSH_MESSAGE_MUTATION,
}
2 changes: 0 additions & 2 deletions packages/codegen/templates/app00/domains/condo/gql/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const botGql = require('./gql.bot')
const clientGql = require('./gql.client')
const { CONDO_CONFIG } = clientGql
const serverGql = require('./gql.server')

module.exports = {
clientGql,
serverGql,
botGql,
CONDO_CONFIG,
}
26 changes: 1 addition & 25 deletions packages/codegen/templates/app00/domains/condo/utils/access.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const get = require('lodash/get')
const isArray = require('lodash/isArray')
const isObject = require('lodash/isObject')

const conf = require('@open-condo/config')

const { hasAllRequiredPermissions } = require('./helpers')
const { OrganizationEmployee, B2BAppRole } = require('./serverSchema')


Expand All @@ -29,36 +26,15 @@ async function getOrganizationPermissions (context, userId, organizationId) {
if (!roleId) return null

const b2bAppRole = await B2BAppRole.getOne(context, {
role: { id: roleId },
role: { id: roleId, deletedAt: null },
app: { id: B2B_APP_ID, deletedAt: null },
deletedAt: null,
})

return get(b2bAppRole, 'permissions', null)
}

async function checkOrganizationPermission (context, userId, organizationId, requiredPermissions = []) {
if (!userId || !organizationId) return false

if (!isArray(requiredPermissions)) {
console.warn('requiredPermissions must be array!')
return false
}

const permissions = await getOrganizationPermissions(context, userId, organizationId)

if (!isObject(permissions)) return false

return hasAllRequiredPermissions(permissions, requiredPermissions)
}

async function checkPermissionInUserOrganization (context, userId, organizationId, requiredPermissions = []) {
if (!userId || !organizationId) return false
return await checkOrganizationPermission(context, userId, organizationId, requiredPermissions)
}


module.exports = {
checkPermissionInUserOrganization,
getOrganizationPermissions,
}

This file was deleted.

Loading

0 comments on commit 01ff192

Please sign in to comment.