Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 4c97602

Browse files
authored
Add CustomerId field (#267)
- update SubscriptionField to work with customer as wel as organisation properties.
1 parent a748ae9 commit 4c97602

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

src/custom-surf/uniforms/AutoFieldLoader.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import VlanField from "custom/uniforms/VlanField";
66
import {
77
AcceptField,
88
BoolField,
9+
CustomerField,
910
DateField,
1011
DividerField,
1112
LabelField,
@@ -59,6 +60,8 @@ export function autoFieldFunction(props: GuaranteedProps<unknown> & Record<strin
5960
return LocationCodeField;
6061
case "organisationId":
6162
return OrganisationField;
63+
case "customerId":
64+
return CustomerField;
6265
case "contactPersonName":
6366
return ContactPersonNameField;
6467
case "vlan":

src/lib/uniforms-surfnet/src/SubscriptionField.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import { filterProductsByBandwidth } from "validations/Products";
3333
import { subscriptionFieldStyling } from "./SubscriptionFieldStyling";
3434

3535
export function makeLabel(subscription: iSubscription, products: Product[], organisations?: Organization[]) {
36-
const organisation = organisations && organisations.find((org) => org.uuid === subscription.customer_id);
37-
const organisationName = organisation ? organisation.name : subscription.customer_id.substring(0, 8);
36+
const customer = organisations && organisations.find((org) => org.uuid === subscription.customer_id);
37+
const customerName = customer ? customer.name : subscription.customer_id.substring(0, 8);
3838
const product = subscription.product || productById(subscription.product_id!, products);
3939
const description =
4040
subscription.description || intl.formatMessage({ id: "forms.widgets.subscription.missingDescription" });
@@ -47,7 +47,7 @@ export function makeLabel(subscription: iSubscription, products: Product[], orga
4747
} else if (["SP", "SPNL", "AGGSP", "AGGSPNL", "MSC", "MSCNL", "IRBSP"].includes(product.tag)) {
4848
let portSubscription = subscription as ServicePortSubscription;
4949
const portMode = getPortMode(portSubscription, products);
50-
return `${subscription_substring} ${portMode.toUpperCase()} ${description.trim()} ${organisationName}`;
50+
return `${subscription_substring} ${portMode.toUpperCase()} ${description.trim()} ${customerName}`;
5151
} else {
5252
return description.trim();
5353
}
@@ -62,6 +62,8 @@ export function getPortMode(subscription: ServicePortSubscription, products: Pro
6262
declare module "uniforms" {
6363
interface FilterDOMProps {
6464
excludedSubscriptionIds: never;
65+
customerId: never;
66+
customerKey: never;
6567
organisationId: never;
6668
organisationKey: never;
6769
visiblePortMode: never;
@@ -74,6 +76,8 @@ declare module "uniforms" {
7476
filterDOMProps.register(
7577
"productIds",
7678
"excludedSubscriptionIds",
79+
"customerId",
80+
"customerKey",
7781
"organisationId",
7882
"organisationKey",
7983
"visiblePortMode",
@@ -88,6 +92,8 @@ export type SubscriptionFieldProps = FieldProps<
8892
{
8993
productIds?: string[];
9094
excludedSubscriptionIds?: string[];
95+
customerId?: string;
96+
customerKey?: string;
9197
organisationId?: string;
9298
organisationKey?: string;
9399
visiblePortMode?: string;
@@ -117,6 +123,8 @@ function Subscription({
117123
className = "",
118124
productIds,
119125
excludedSubscriptionIds,
126+
customerId,
127+
customerKey,
120128
organisationId,
121129
organisationKey,
122130
visiblePortMode = "all",
@@ -128,6 +136,13 @@ function Subscription({
128136
intl,
129137
...props
130138
}: SubscriptionFieldProps) {
139+
if (organisationId) {
140+
customerId = organisationId;
141+
}
142+
if (organisationKey) {
143+
customerKey = organisationKey;
144+
}
145+
131146
const { theme, organisations, products: allProducts, apiClient, customApiClient } = useContext(ApplicationContext);
132147
const { getSubscriptions, clearSubscriptions } = useContext(SubscriptionsContext);
133148

@@ -154,10 +169,8 @@ function Subscription({
154169

155170
const usedBandwidth = bandwidth || bandwithFromField;
156171

157-
// Get value from org field if organisationKey is set.
158-
const usedOrganisationId = organisationKey
159-
? get(model, organisationKey, "nonExistingOrgToFilterEverything")
160-
: organisationId;
172+
// Get value from org field if customerKey is set.
173+
const usedOrganisationId = customerKey ? get(model, customerKey, "nonExistingOrgToFilterEverything") : customerId;
161174

162175
const filteredProductIds = useMemo(() => {
163176
let products = allProducts;

src/lib/uniforms-surfnet/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ export { default as SummaryField } from "lib/uniforms-surfnet/src/SummaryField";
4343

4444
export { default as LocationCodeField } from "lib/uniforms-surfnet/src/logic/LocationCodeField";
4545
export { default as OrganisationField } from "lib/uniforms-surfnet/src/logic/OrganisationField";
46+
export { default as CustomerField } from "lib/uniforms-surfnet/src/logic/CustomerField";
4647
export { default as ProductField } from "lib/uniforms-surfnet/src/logic/ProductField";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019-2023 SURF.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
*/
15+
16+
import SelectField, { SelectFieldProps } from "lib/uniforms-surfnet/src/SelectField";
17+
import get from "lodash/get";
18+
import React, { useContext } from "react";
19+
import { useIntl } from "react-intl";
20+
import { connectField } from "uniforms";
21+
import ApplicationContext from "utils/ApplicationContext";
22+
23+
export type CustomerFieldProps = Omit<SelectFieldProps, "placeholder" | "transform" | "allowedValues">;
24+
25+
function Customer({ name, ...props }: CustomerFieldProps) {
26+
const intl = useIntl();
27+
const { organisations } = useContext(ApplicationContext);
28+
const customerLabelLookup =
29+
organisations?.reduce<{ [index: string]: string }>(function (mapping, org) {
30+
mapping[org.uuid] = org.name;
31+
return mapping;
32+
}, {}) ?? {};
33+
34+
return (
35+
<SelectField
36+
name=""
37+
{...props}
38+
allowedValues={Object.keys(customerLabelLookup)}
39+
transform={(uuid: string) => get(customerLabelLookup, uuid, uuid)}
40+
placeholder={intl.formatMessage({ id: "forms.widgets.customer.placeholder" })}
41+
/>
42+
);
43+
}
44+
45+
export default connectField(Customer);

src/locale/en.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ I18n.translations.en = {
107107
organisation: {
108108
placeholder: "Search and select a customer...",
109109
},
110+
customer: {
111+
placeholder: "Search and select a customer...",
112+
},
110113
product: {
111114
placeholder: "Search and select a product...",
112115
},

0 commit comments

Comments
 (0)