@bindu-dashing/dam-solution-v2 5.8.161 → 5.8.164

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,10 +12,10 @@ import { Button, Checkbox, Drawer, Form, Input, Select, Typography } from "antd"
12
12
  import { useEffect, useMemo, useState } from "react";
13
13
  import { ACCESS_TYPES, DAM_LOCATION_TYPES } from "../hocs/appConstants";
14
14
  import { useDamConfig } from "../hocs/DamConfigContext";
15
- import { CREATE_SUB_BRAND, FETCH_BRAND_USING_SUBDOMAIN } from "../utilities/constants/apiUrls";
15
+ import { CREATE_SUB_BRAND, FETCH_BRAND_USING_SUBDOMAIN, USER_LOGIN } from "../utilities/constants/apiUrls";
16
16
  import axios from "axios";
17
17
  import { createApiClient } from "../hocs/configureAxios";
18
- import { fetchBrandDetails } from "../react-query/services/brand-services";
18
+ import { getBaseUrl } from "../hocs/helpers";
19
19
  import { CREATE_SUCCESS, SOMETHING_WENT_WRONG, } from "../utilities/constants/messages";
20
20
  import { NotificationStatus } from "../utilities/constants/interface";
21
21
  import { showNotification } from "../common/notifications";
@@ -29,6 +29,8 @@ const CreateClientForm = ({ teamsApi, username, password, toggleShow, onSuccess,
29
29
  const [filteredTeams, setFilteredTeams] = useState([]);
30
30
  const [clientData, setClientData] = useState(null);
31
31
  const [teamsFetched, setTeamsFetched] = useState(false);
32
+ const [authToken, setAuthToken] = useState(null);
33
+ const [brandIdForUpdate, setBrandIdForUpdate] = useState(null);
32
34
  const api = useMemo(() => createApiClient(damConfig), [damConfig]);
33
35
  const [form] = Form.useForm();
34
36
  const isEditMode = !!existingClientData;
@@ -39,11 +41,21 @@ const CreateClientForm = ({ teamsApi, username, password, toggleShow, onSuccess,
39
41
  // Fetch client data when editing
40
42
  useEffect(() => {
41
43
  const fetchClientData = () => __awaiter(void 0, void 0, void 0, function* () {
44
+ var _a, _b, _c, _d;
42
45
  if (isEditMode) {
43
46
  setFetchingClientData(true);
44
47
  try {
48
+ // Validate that we have required credentials
49
+ if (!(existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.accessKey) || !(existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.secretKey) || !(existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.subdomain)) {
50
+ console.error("Missing required credentials for login:", {
51
+ accessKey: !!(existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.accessKey),
52
+ secretKey: !!(existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.secretKey),
53
+ subdomain: !!(existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.subdomain)
54
+ });
55
+ setFetchingClientData(false);
56
+ return;
57
+ }
45
58
  let fetchedData;
46
- // TEMPORARY: Using static brandId for testing - remove after testing
47
59
  let brandId = (existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.brandId) || "68a2f214f7e91fbee730b0b9";
48
60
  console.log("Edit mode - existingClientData:", existingClientData);
49
61
  console.log("Using brandId:", brandId);
@@ -63,22 +75,68 @@ const CreateClientForm = ({ teamsApi, username, password, toggleShow, onSuccess,
63
75
  console.warn("Failed to fetch by subdomain, using static brandId:", subdomainError);
64
76
  }
65
77
  }
66
- // Always use brands/details endpoint with brandId
67
- if (brandId) {
68
- console.log("Fetching brand details using brandId:", brandId);
69
- console.log("API endpoint:", `/brands/details/${brandId}`);
70
- // Use reusable fetchBrandDetails function
71
- fetchedData = yield fetchBrandDetails(api, brandId);
72
- console.log("Fetched client data from brands/details:", fetchedData);
78
+ // Step 1: Call login API first with access and secret keys
79
+ const baseUrl = getBaseUrl(appType);
80
+ const loginPayload = {
81
+ accessKey: existingClientData.accessKey,
82
+ secretKey: existingClientData.secretKey,
83
+ subdomain: existingClientData.subdomain,
84
+ teams: [brandId] // Use brandId as team ID
85
+ };
86
+ console.log("Step 1: Calling login API with payload:", Object.assign(Object.assign({}, loginPayload), { secretKey: loginPayload.secretKey ? `${loginPayload.secretKey.substring(0, 10)}...` : 'missing' }));
87
+ let token;
88
+ let loginBrandId = brandId;
89
+ try {
90
+ const loginResponse = yield axios.post(`${baseUrl}${USER_LOGIN}`, loginPayload, {
91
+ headers: {
92
+ 'Content-Type': 'application/json',
93
+ 'accept': 'application/json, text/plain, */*'
94
+ }
95
+ });
96
+ console.log("Login response:", loginResponse.data);
97
+ token = (_b = (_a = loginResponse === null || loginResponse === void 0 ? void 0 : loginResponse.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.access_token;
98
+ const loginBrandData = (_d = (_c = loginResponse === null || loginResponse === void 0 ? void 0 : loginResponse.data) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.brand;
99
+ loginBrandId = (loginBrandData === null || loginBrandData === void 0 ? void 0 : loginBrandData._id) || brandId;
100
+ if (!token) {
101
+ console.error("No token received from login API");
102
+ setFetchingClientData(false);
103
+ return;
104
+ }
105
+ // Store token and brandId for use in update API
106
+ setAuthToken(token);
107
+ setBrandIdForUpdate(loginBrandId);
73
108
  }
74
- else if (existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.subdomain) {
75
- console.log("No brandId available, using subdomain endpoint:", existingClientData.subdomain);
76
- const response = yield api.get(`${FETCH_BRAND_USING_SUBDOMAIN}?subdomain=${existingClientData.subdomain}`);
77
- fetchedData = get(response, "data.data", {});
78
- console.log("Fetched client data from subdomain:", fetchedData);
109
+ catch (loginError) {
110
+ console.error("Error logging in with DAM credentials:", loginError);
111
+ if (loginError.response) {
112
+ console.error("Login error response:", loginError.response.data);
113
+ console.error("Login error status:", loginError.response.status);
114
+ }
115
+ setFetchingClientData(false);
116
+ return;
79
117
  }
80
- else {
81
- console.warn("No brandId or subdomain provided");
118
+ // Step 2: Call brands/details API with the token (only after login succeeds)
119
+ console.log("Step 2: Calling brands/details API with token and brandId:", loginBrandId);
120
+ try {
121
+ const brandDetailsResponse = yield axios.get(`${baseUrl}/brands/details/${loginBrandId}`, {
122
+ headers: {
123
+ 'Authorization': `Bearer ${token}`,
124
+ 'accept': 'application/json, text/plain, */*'
125
+ }
126
+ });
127
+ fetchedData = get(brandDetailsResponse, "data.data", {});
128
+ console.log("Fetched client data from brands/details:", fetchedData);
129
+ }
130
+ catch (brandDetailsError) {
131
+ console.error("Error fetching brand details:", brandDetailsError);
132
+ if (brandDetailsError.response) {
133
+ console.error("Brand details error response:", brandDetailsError.response.data);
134
+ console.error("Brand details error status:", brandDetailsError.response.status);
135
+ }
136
+ // If brand details fetch fails, still set subdomain
137
+ if (existingClientData === null || existingClientData === void 0 ? void 0 : existingClientData.subdomain) {
138
+ form.setFieldValue("subdomain", existingClientData.subdomain);
139
+ }
82
140
  setFetchingClientData(false);
83
141
  return;
84
142
  }
@@ -151,7 +209,26 @@ const CreateClientForm = ({ teamsApi, username, password, toggleShow, onSuccess,
151
209
  rootPath: (_h = data === null || data === void 0 ? void 0 : data.damLocationDetails) === null || _h === void 0 ? void 0 : _h.rootPath,
152
210
  } });
153
211
  setLoading(true);
154
- const response = yield api.post(CREATE_SUB_BRAND, values);
212
+ let response;
213
+ const baseUrl = getBaseUrl(appType);
214
+ if (isEditMode && brandIdForUpdate && authToken) {
215
+ // Update existing client: PUT to /brands/{brandId}
216
+ console.log("Updating client with brandId:", brandIdForUpdate);
217
+ console.log("Update payload:", values);
218
+ const putResponse = yield axios.put(`${baseUrl}/brands/${brandIdForUpdate}`, values, {
219
+ headers: {
220
+ 'Authorization': `Bearer ${authToken}`,
221
+ 'Content-Type': 'application/json',
222
+ 'accept': 'application/json, text/plain, */*'
223
+ }
224
+ });
225
+ // Extract data from axios response (API returns { data: {...} } or { data: { data: {...} } })
226
+ response = get(putResponse, "data.data", get(putResponse, "data", putResponse));
227
+ }
228
+ else {
229
+ // Create new client: POST to /brands/sub-brand
230
+ response = yield api.post(CREATE_SUB_BRAND, values);
231
+ }
155
232
  setLoading(false);
156
233
  showNotification((_j = response === null || response === void 0 ? void 0 : response.message) !== null && _j !== void 0 ? _j : (get(response, "message") || CREATE_SUCCESS), NotificationStatus.SUCCESS);
157
234
  toggleShow();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bindu-dashing/dam-solution-v2",
3
- "version": "5.8.161",
3
+ "version": "5.8.164",
4
4
  "dependencies": {
5
5
  "@ant-design/icons": "^5.0.1",
6
6
  "@emoji-mart/data": "^1.2.1",