Skip to content

Commit

Permalink
Merge pull request #95 from syatt-io/feature/custom-fields
Browse files Browse the repository at this point in the history
Custom fields
  • Loading branch information
arturmagalhaesjr authored Feb 14, 2023
2 parents 83c5b05 + 047d69b commit 15abf3a
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Added custom fields for organizations and cost centers

## [0.28.0] - 2023-02-08

### Added
Expand Down
50 changes: 50 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type Mutation {
collections: [CollectionInput]
paymentTerms: [PaymentTermInput]
priceTables: [String]
customFields: [CustomFieldInput]
salesChannel: String
sellers: [SellerInput]
notifyUsers: Boolean
Expand Down Expand Up @@ -274,6 +275,7 @@ type DefaultCostCenter {
address: Address
phoneNumber: String
businessDocument: String
customFields: [CustomField]
stateRegistration: String
}

Expand All @@ -285,6 +287,7 @@ type Organization {
paymentTerms: [PaymentTerm]
priceTables: [String]
sellers: [Seller]
customFields: [CustomField]
salesChannel: String
costCenters: [ID]
status: String
Expand All @@ -309,6 +312,7 @@ type CostCenter {
paymentTerms: [PaymentTerm]
phoneNumber: String
businessDocument: String
customFields: [CustomField]
stateRegistration: String
}

Expand Down Expand Up @@ -399,6 +403,8 @@ type B2BSettings {
autoApprove: Boolean
defaultPaymentTerms: [PaymentTerm]
defaultPriceTables: [String]
organizationCustomFields: [SettingsCustomField]
costCenterCustomFields: [SettingsCustomField]
uiSettings: UISettings
}

Expand All @@ -419,6 +425,7 @@ input OrganizationInput {
priceTables: [String]
salesChannel: String
sellers: [SellerInput]
customFields: [CustomFieldInput]
}

input B2BUserInput {
Expand All @@ -432,6 +439,7 @@ input DefaultCostCenterInput {
address: AddressInput
phoneNumber: String
businessDocument: String
customFields: [CustomFieldInput]
stateRegistration: String
}

Expand All @@ -441,6 +449,7 @@ input CostCenterInput {
paymentTerms: [PaymentTermInput]
phoneNumber: String
businessDocument: String
customFields: [CustomFieldInput]
stateRegistration: String
}

Expand Down Expand Up @@ -476,6 +485,35 @@ input PaymentTermInput {
name: String
}

enum CustomFieldType {
text
dropdown
}

type CustomField {
name: String
type: CustomFieldType
value: String
dropdownValues: [DropdownValue]
useOnRegistration: Boolean
}

type SettingsCustomField {
name: String
type: CustomFieldType
dropdownValues: [DropdownValue]
useOnRegistration: Boolean
}

input CustomFieldInput {
name: String!
type: CustomFieldType!
value: String
dropdownValues: [DropdownValueInput]
useOnRegistration: Boolean
}


input SalesChannelsInput {
id: String
name: String
Expand All @@ -491,4 +529,16 @@ input B2BSettingsInput {
defaultPaymentTerms: [PaymentTermInput]
defaultPriceTables: [String]
uiSettings: UISettingsInput
organizationCustomFields: [CustomFieldInput]
costCenterCustomFields: [CustomFieldInput]
}

type DropdownValue {
value: String
label: String
}

input DropdownValueInput {
value: String
label: String
}
10 changes: 10 additions & 0 deletions node/mdSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const ORGANIZATION_FIELDS = [
'sellers',
'status',
'created',
'customFields',
]
export const ORGANIZATION_SCHEMA_VERSION = 'v0.0.8'

Expand All @@ -41,6 +42,7 @@ export const COST_CENTER_FIELDS = [
'organization',
'phoneNumber',
'businessDocument',
'customFields',
'stateRegistration',
]
export const COST_CENTER_SCHEMA_VERSION = 'v0.0.7'
Expand Down Expand Up @@ -155,6 +157,10 @@ export const schemas = [
title: 'Created',
format: 'date-time',
},
customFields: {
type: 'array',
title: 'Custom Fields',
},
},
'v-indexed': ['name', 'status', 'created'],
'v-immediate-indexing': true,
Expand Down Expand Up @@ -194,6 +200,10 @@ export const schemas = [
type: ['string', 'null'],
title: 'Phone Number',
},
customFields: {
type: ['array', 'null'],
title: 'Custom Fields',
},
},
'v-indexed': [
'name',
Expand Down
6 changes: 6 additions & 0 deletions node/resolvers/Mutations/CostCenters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const CostCenters = {
phoneNumber,
businessDocument,
stateRegistration,
customFields,
},
}: { organizationId: string; input: CostCenterInput },
ctx: Context
Expand Down Expand Up @@ -53,6 +54,7 @@ const CostCenters = {
...(phoneNumber && { phoneNumber }),
...(businessDocument && { businessDocument }),
...(stateRegistration && { stateRegistration }),
...(customFields && { customFields }),
}

const createCostCenterResult = await masterdata.createDocument({
Expand Down Expand Up @@ -162,6 +164,7 @@ const CostCenters = {
phoneNumber,
businessDocument,
stateRegistration,
customFields,
},
}: { id: string; input: CostCenterInput },
ctx: Context
Expand All @@ -188,6 +191,9 @@ const CostCenters = {
...((businessDocument || businessDocument === '') && {
businessDocument,
}),
...((customFields || customFields === '') && {
customFields,
}),
},
id,
})
Expand Down
13 changes: 13 additions & 0 deletions node/resolvers/Mutations/Organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Organizations = {
tradeName,
defaultCostCenter,
costCenters,
customFields,
paymentTerms,
priceTables,
salesChannel,
Expand Down Expand Up @@ -55,6 +56,7 @@ const Organizations = {
...(paymentTerms?.length && { paymentTerms }),
...(priceTables?.length && { priceTables }),
...(salesChannel && { salesChannel }),
customFields: customFields ?? [],
status: ORGANIZATION_STATUSES.ACTIVE,
}

Expand All @@ -78,6 +80,9 @@ const Organizations = {
...(data?.businessDocument && {
businessDocument: data?.businessDocument,
}),
...(defaultCostCenter?.customFields && {
customFields: defaultCostCenter.customFields,
}),
...(data?.stateRegistration && {
stateRegistration: data?.stateRegistration,
}),
Expand Down Expand Up @@ -129,6 +134,7 @@ const Organizations = {
b2bCustomerAdmin,
costCenters,
defaultCostCenter,
customFields,
name,
tradeName,
priceTables,
Expand Down Expand Up @@ -174,6 +180,7 @@ const Organizations = {
b2bCustomerAdmin,
costCenters,
created: now,
customFields,
defaultCostCenter:
defaultCostCenter ?? (costCenters?.length && costCenters[0]),
notes: '',
Expand Down Expand Up @@ -244,6 +251,7 @@ const Organizations = {
collections,
paymentTerms,
priceTables,
customFields,
salesChannel,
sellers,
notifyUsers = true,
Expand All @@ -255,6 +263,7 @@ const Organizations = {
collections: any[]
paymentTerms: any[]
priceTables: any[]
customFields: any[]
salesChannel?: string
sellers?: any[]
notifyUsers?: boolean
Expand Down Expand Up @@ -299,6 +308,7 @@ const Organizations = {
collections,
paymentTerms,
priceTables,
customFields,
...(salesChannel && { salesChannel }),
...(sellers && { sellers }),
status,
Expand Down Expand Up @@ -402,6 +412,9 @@ const Organizations = {
...(defaultCostCenter.businessDocument && {
businessDocument: defaultCostCenter.businessDocument,
}),
...(defaultCostCenter.customFields && {
customFields: defaultCostCenter.customFields,
}),
},
name,
paymentTerms,
Expand Down
36 changes: 36 additions & 0 deletions node/resolvers/Mutations/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const Settings = {
defaultPaymentTerms,
defaultPriceTables,
uiSettings,
organizationCustomFields,
costCenterCustomFields,
},
}: {
input: B2BSettingsInput
Expand All @@ -53,11 +55,45 @@ const Settings = {

const B2B_SETTINGS_DATA_ENTITY = 'b2b_settings'

// Get current settings to save them depending on where the saveB2BSettings is coming from. Custom fields come without autoApprove settings and vice versa.

let currentB2BSettings: B2BSettingsInput | undefined

try {
const getSettingsObj: B2BSettingsInput = await vbase.getJSON(
B2B_SETTINGS_DATA_ENTITY,
'settings'
)

currentB2BSettings = getSettingsObj
} catch (e) {
if (e.response?.status === 404) {
// when run for the first time on the project - returns 404. ignore in that case
} else {
logger.error({
message: 'saveB2BSettings-currentB2BSettings-error',
error: e,
})
if (e.message) {
throw new GraphQLError(e.message)
} else if (e.response?.data?.message) {
throw new GraphQLError(e.response.data.message)
} else {
throw new GraphQLError(e)
}
}
}

try {
const b2bSettings = {
autoApprove,
costCenterCustomFields:
costCenterCustomFields ?? currentB2BSettings?.costCenterCustomFields,
defaultPaymentTerms,
defaultPriceTables,
organizationCustomFields:
organizationCustomFields ??
currentB2BSettings?.organizationCustomFields,
uiSettings,
}

Expand Down
9 changes: 8 additions & 1 deletion node/resolvers/Queries/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ const B2BSettings = {
// create schema if it doesn't exist
await checkConfig(ctx)

let settings = null
let settings: Partial<B2BSettingsInput> | null = null

try {
settings = await vbase.getJSON<B2BSettingsInput | null>(
B2B_SETTINGS_DATA_ENTITY,
'settings',
true
)

settings = {
...settings,
// if custom fields are null, set to an empty array
costCenterCustomFields: settings?.costCenterCustomFields ?? [],
organizationCustomFields: settings?.organizationCustomFields ?? [],
}
} catch (e) {
if (e.message) {
throw new GraphQLError(e.message)
Expand Down
Loading

0 comments on commit 15abf3a

Please sign in to comment.