Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 98 additions & 90 deletions api/src/services/contentMapper.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Request } from "express";
import ProjectModel from "../models/project.js";
import { getLogMessage, isEmpty, safePromise } from "../utils/index.js";
import {
BadRequestError,
Expand All @@ -8,8 +7,6 @@ import {
import {
HTTP_TEXTS,
HTTP_CODES,
POPULATE_CONTENT_MAPPER,
POPULATE_FIELD_MAPPING,
PROJECT_STATUS,
STEPPER_STEPS,
} from "../constants/index.js";
Expand Down Expand Up @@ -457,14 +454,7 @@ const resetToInitialMapping = async (req: Request) => {
};
const resetAllContentTypesMapping = async (projectId: string) => {
const srcFunc = "resetAllContentTypesMapping";
// const projectId = req?.params?.projectId;

// const projectDetails: any = await ProjectModel.findOne({
// _id: projectId,
// }).populate({
// path: POPULATE_CONTENT_MAPPER,
// populate: { path: POPULATE_FIELD_MAPPING },
// });

await ProjectModelLowdb.read();
const projectDetails = ProjectModelLowdb.chain
.get("projects")
Expand All @@ -481,15 +471,6 @@ const resetAllContentTypesMapping = async (projectId: string) => {
);
throw new BadRequestError(HTTP_TEXTS.CONTENTMAPPER_NOT_FOUND);
}
await ContentTypesMapperModelLowdb.read();
const data = contentMapperId.map((id: any) => {
const getFeildMappingData = ContentTypesMapperModelLowdb.chain
.get("ContentTypesMappers")
.find({ id: id })
.value();
return getFeildMappingData;
});

if (isEmpty(projectDetails)) {
logger.error(
getLogMessage(
Expand All @@ -499,48 +480,57 @@ const resetAllContentTypesMapping = async (projectId: string) => {
);
throw new BadRequestError(HTTP_TEXTS.PROJECT_NOT_FOUND);
}
await ContentTypesMapperModelLowdb.read();
const cData = contentMapperId.map((cId: any) => {
const contentTypeData = ContentTypesMapperModelLowdb.chain
.get("ContentTypesMappers")
.find({ id: cId })
.value();
return contentTypeData;
});

try {
// const contentTypes = projectDetails?.content_mapper;
const contentTypes = data;
const contentTypesbulkWriteOperations: any = await Promise.all(
contentTypes?.map(async (contentType: any) => {
if (!isEmpty(contentType?.fieldMapping)) {
const contentTypes = cData;
for (const contentType of contentTypes) {
if (!isEmpty(contentType.fieldMapping)) {
for (const field of contentType.fieldMapping) {
await FieldMapperModel.read();
(contentType?.fieldMapping || []).forEach((field: any) => {
const fieldIndex = FieldMapperModel.data.field_mapper.findIndex(
(f: any) => f?.id === field?.id
);
if (fieldIndex > -1) {
FieldMapperModel.update((data: any) => {
data.field_mapper[fieldIndex] = {
...field,
contentstackField: "",
contentstackFieldUid: "",
ContentstackFieldType: field.backupFieldType,
};
});
}
const fieldData = FieldMapperModel.chain
.get("field_mapper")
.find({ id: field })
.value();
const fieldIndex = FieldMapperModel.chain
.get("field_mapper")
.findIndex({ id: field })
.value();

if (fieldIndex > -1) {
await FieldMapperModel.update((fData: any) => {
fData.field_mapper[fieldIndex] = {
...fieldData,
contentstackField: "",
contentstackFieldUid: "",
ContentstackFieldType: fieldData.backupFieldType,
};
});
}
}
}
await ContentTypesMapperModelLowdb.read();
if (!isEmpty(contentType?.id)) {
const cIndex = ContentTypesMapperModelLowdb.chain
.get("ContentTypesMappers")
.findIndex({ id: contentType?.id })
.value();
if (cIndex > -1) {
await ContentTypesMapperModelLowdb.update((data: any) => {
data.ContentTypesMappers[cIndex].contentstackTitle = "";
data.ContentTypesMappers[cIndex].contentstackUid = "";
});
}
return {
updateOne: {
filter: { _id: contentType._id },
update: {
$set: {
contentstackTitle: "",
contentstackUid: "",
},
},
},
};
})
);
}
}

await ContentTypesMapperModelLowdb.read();
ContentTypesMapperModelLowdb.update((data: any) => {
data.ContentTypesMappers.push(contentTypesbulkWriteOperations);
});
return projectDetails;
} catch (error: any) {
logger.error(
Expand All @@ -559,13 +549,11 @@ const resetAllContentTypesMapping = async (projectId: string) => {
};
const removeMapping = async (projectId: string) => {
const srcFunc = "removeMapping";

const projectDetails: any = await ProjectModel.findOne({
_id: projectId,
}).populate({
path: POPULATE_CONTENT_MAPPER,
populate: { path: POPULATE_FIELD_MAPPING },
});
await ProjectModelLowdb.read();
const projectDetails = ProjectModelLowdb.chain
.get("projects")
.find({ id: projectId })
.value();

if (isEmpty(projectDetails)) {
logger.error(
Expand All @@ -576,40 +564,59 @@ const removeMapping = async (projectId: string) => {
);
throw new BadRequestError(HTTP_TEXTS.PROJECT_NOT_FOUND);
}
await ContentTypesMapperModelLowdb.read();
const cData = projectDetails?.content_mapper.map((cId: any) => {
const contentTypeData = ContentTypesMapperModelLowdb.chain
.get("ContentTypesMappers")
.find({ id: cId })
.value();
return contentTypeData;
});

try {
const contentTypes = projectDetails?.content_mapper;

const contentTypes = cData;
//TODO: remove fieldMapping ids in ContentTypesMapperModel for each content types
const contentTypesbulkWriteOperations: any = await Promise.all(
contentTypes?.map(async (contentType: any) => {
if (!isEmpty(contentType?.fieldMapping)) {

for (const contentType of contentTypes) {
if (!isEmpty(contentType.fieldMapping)) {
for (const field of contentType.fieldMapping) {
await FieldMapperModel.read();
(contentType?.fieldMapping || []).forEach((field: any) => {
const fieldIndex = FieldMapperModel.data.field_mapper.findIndex(
(f: any) => f?.id === field?.id
);
if (fieldIndex > -1) {
FieldMapperModel.update((data: any) => {
delete data.field_mapper[fieldIndex];
});
}
const fieldIndex = FieldMapperModel.chain
.get("field_mapper")
.findIndex({ id: field })
.value();
if (fieldIndex > -1) {
await FieldMapperModel.update((fData: any) => {
delete fData.field_mapper[fieldIndex];
});
}
}
}
await ContentTypesMapperModelLowdb.read();
if (!isEmpty(contentType?.id)) {
const cIndex = ContentTypesMapperModelLowdb.chain
.get("ContentTypesMappers")
.findIndex({ id: contentType?.id })
.value();
if (cIndex > -1) {
await ContentTypesMapperModelLowdb.update((data: any) => {
delete data.ContentTypesMappers[cIndex];
});
}
return {
deleteOne: {
filter: { _id: contentType._id },
},
};
})
);
}
}

await ContentTypesMapperModelLowdb.read();
ContentTypesMapperModelLowdb.update((data: any) => {
data.ContentTypesMappers.push(contentTypesbulkWriteOperations);
});
projectDetails.content_mapper = [];
await projectDetails?.save();
await ProjectModelLowdb.read();
const projectIndex = ProjectModelLowdb.chain
.get("projects")
.findIndex({ id: projectId })
.value();

if (projectIndex > -1) {
ProjectModelLowdb.update((data: any) => {
data.projects[projectIndex].content_mapper = [];
});
}
return projectDetails;
} catch (error: any) {
logger.error(
Expand All @@ -626,6 +633,7 @@ const removeMapping = async (projectId: string) => {
);
}
};

export const contentMapperService = {
putTestData,
getContentTypes,
Expand Down
7 changes: 7 additions & 0 deletions ui/src/components/ContentMapper/contentMapper.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface FieldMapType {
otherCmsType: string;
uid: string;
_id: string;
_invalid?: boolean;
}

export interface ItemStatus {
Expand Down Expand Up @@ -88,3 +89,9 @@ export interface ContentTypeList {
title: string;
schema: [];
}

export interface optionsType {
label?: string;
value?: string;
isDisabled?: boolean;
}
14 changes: 11 additions & 3 deletions ui/src/components/ContentMapper/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
}
.ct-list {
list-style-type: none;
margin-right: 15px;
margin-top: 5px;
margin: 5px 15px 0;
li {
border: 1px solid transparent;
cursor: pointer;
font-size: $size-font-large;
line-height: normal;
padding: $space-10 35px;
&.active-ct {
align-items: center;
background-color: $color-brand-white-base;
border: 1px solid $color-brand-primary-base;
color: $color-brand-primary-base;
display: flex;
justify-content: space-between;
}
}
}
Expand All @@ -44,6 +46,7 @@
}
.cta-wrapper {
border-top: 1px solid $color-base-gray-40;
flex: 1 0 auto;
// bottom: 0;
padding: $space-12 $space-24 $space-12 $space-12;
// position: fixed;
Expand All @@ -53,6 +56,9 @@
z-index: 1;
position: relative;
}
.table-container {
flex: 1 0 auto;
}
.table-wrapper {
.Table {
min-height: inherit;
Expand All @@ -65,10 +71,12 @@
padding: 12px;
}
.step-container {
display: flex;
flex-direction: column;
margin-top: 0;
width: 100%;
margin-left: 0px;
margin-bottom: 20px;
width: 100%;
}
.saveButton {
padding: 5px 30px;
Expand Down
Loading