Skip to content
Closed
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
31 changes: 31 additions & 0 deletions src/models/contentTypesMapper-lowdb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { JSONFile } from "lowdb/node";
import LowWithLodash from "../utils/lowdb-lodash.utils.js";

interface contentTypes {
otherCmsTitle: string;
otherCmsUid: string;
isUpdated: boolean;
updateAt: Date;
contentstackTitle: string;
contentstackUid: string;
fieldMapping: [];
}

interface ContentTypesMapper {
id: string;
projectId: string;
contentTypes: [contentTypes];
}

interface ContentTypeMapperDocument {
ContentTypesMappers: ContentTypesMapper[];
}

const defaultData: ContentTypeMapperDocument = { ContentTypesMappers: [] };

const db = new LowWithLodash(
new JSONFile<ContentTypeMapperDocument>("database/contentTypesMapper.json"),
defaultData
);

export default db;
171 changes: 94 additions & 77 deletions src/services/contentMapper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ExceptionFunction,
} from "../utils/custom-errors.utils.js";
import {
CONTENT_TYPE_POPULATE_FIELDS,
HTTP_TEXTS,
HTTP_CODES,
POPULATE_CONTENT_MAPPER,
Expand All @@ -23,51 +22,46 @@ import getProjectUtil from "../utils/get-project.utils.js";
import ProjectModelLowdb from "../models/project-lowdb.js";
import FieldMapperModel from "../models/FieldMapper.js";
import { v4 as uuidv4 } from "uuid";
import ContentTypesMapperModelLowdb from "../models/contentTypesMapper-lowdb.js";

// Developer service to create dummy contentmapping data
const putTestData = async (req: Request) => {
const projectId = req.params.projectId;
const contentTypes = req.body;
const contentTypes = req.body.contentTypes;

await FieldMapperModel.read();
contentTypes.map((type: any, index: any) => {
const fieldIds: string[] = [];
const fields = type.fieldMapping.map((field: any) => {
const id = uuidv4();
fieldIds.push(id);
return { id, isDeleted: false, ...field.fieldMapping };
return { id, isDeleted: false, ...field };
});

FieldMapperModel.update((data: any) => {
data.field_mapper = [...data.field_mapper, ...fields];
});

contentTypes[index].fieldMapping = fieldIds;
contentTypes[index].fieldMapping = fields;
});

let typeIds: any = [];

await ContentTypesMapperModel.insertMany(contentTypes, {
ordered: true,
})
.then(async function (docs) {
// do something with docs
typeIds = docs.map((item) => {
return item._id;
});
})
.catch(function () {
// console.log(err)
// error handling here
});

const projectDetails: any = await ProjectModel.findOne({
_id: projectId,
const typeIds: any = [];
const obj = {
id: uuidv4(),
projectId: projectId,
contentTypes: contentTypes,
};
await ContentTypesMapperModelLowdb.read();
await ContentTypesMapperModelLowdb.update((data: any) => {
data.ContentTypesMappers.push(obj);
});
projectDetails.content_mapper = typeIds;
projectDetails.save();
//Add logic to get Project from DB
return projectDetails;
typeIds.push(obj.id);
await ProjectModelLowdb.read();
const pData = ProjectModelLowdb.chain
.get("projects")
.find({ id: projectId })
.assign({ content_mapper: typeIds })
.value();

return pData;
};

const getContentTypes = async (req: Request) => {
Expand All @@ -77,14 +71,14 @@ const getContentTypes = async (req: Request) => {
const limit: any = req?.params?.limit;
const search: string = req?.params?.searchText?.toLowerCase();

let result = [];
let result: any = [];
let totalCount = 0;
const projectDetails = await ProjectModel.findOne({
_id: projectId,
}).populate({
path: POPULATE_CONTENT_MAPPER,
select: CONTENT_TYPE_POPULATE_FIELDS,
});

await ProjectModelLowdb.read();
const projectDetails = ProjectModelLowdb.chain
.get("projects")
.find({ id: projectId })
.value();

if (isEmpty(projectDetails)) {
logger.error(
Expand All @@ -95,28 +89,35 @@ const getContentTypes = async (req: Request) => {
);
throw new BadRequestError(HTTP_TEXTS.PROJECT_NOT_FOUND);
}
const { content_mapper }: any = projectDetails;

if (!isEmpty(content_mapper)) {
if (search) {
const filteredResult = content_mapper
.filter((item: any) =>
item?.otherCmsTitle?.toLowerCase().includes(search)
)
?.sort((a: any, b: any) =>
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
);
totalCount = filteredResult.length;
result = filteredResult.slice(skip, Number(skip) + Number(limit));
} else {
totalCount = content_mapper.length;
result = content_mapper
?.sort((a: any, b: any) =>
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
)
?.slice(skip, Number(skip) + Number(limit));
const contentMapperId = projectDetails.content_mapper;
await ContentTypesMapperModelLowdb.read();
contentMapperId.map((data: any) => {
const contentMapperData = ContentTypesMapperModelLowdb.chain
.get("ContentTypesMappers")
.find({ id: data })
.value();
const content_mapper = contentMapperData.contentTypes;
if (!isEmpty(content_mapper)) {
if (search) {
const filteredResult = content_mapper
.filter((item: any) =>
item?.otherCmsTitle?.toLowerCase().includes(search)
)
?.sort((a: any, b: any) =>
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
);
totalCount = filteredResult.length;
result = filteredResult.slice(skip, Number(skip) + Number(limit));
} else {
totalCount = content_mapper.length;
result = content_mapper
?.sort((a: any, b: any) =>
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
)
?.slice(skip, Number(skip) + Number(limit));
}
}
}
});
return { count: totalCount, contentTypes: result };
};

Expand All @@ -127,13 +128,16 @@ const getFieldMapping = async (req: Request) => {
const limit: any = req?.params?.limit;
const search: string = req?.params?.searchText?.toLowerCase();

let result = [];
let result: any[] = [];
let filteredResult = [];
let totalCount = 0;

const contentType = await ContentTypesMapperModel.findOne({
_id: contentTypeId,
}).populate("fieldMapping");
await ContentTypesMapperModelLowdb.read();

const contentType = ContentTypesMapperModelLowdb.chain
.get("ContentTypesMappers")
.find({ id: contentTypeId })
.value();

if (isEmpty(contentType)) {
logger.error(
Expand All @@ -145,19 +149,24 @@ const getFieldMapping = async (req: Request) => {
throw new BadRequestError(HTTP_TEXTS.CONTENT_TYPE_NOT_FOUND);
}

const { fieldMapping }: any = contentType;
if (!isEmpty(fieldMapping)) {
if (search) {
filteredResult = fieldMapping.filter((item: any) =>
item?.otherCmsField?.toLowerCase().includes(search)
);
totalCount = filteredResult.length;
result = filteredResult.slice(skip, Number(skip) + Number(limit));
} else {
totalCount = fieldMapping.length;
result = fieldMapping.slice(skip, Number(skip) + Number(limit));
contentType.contentTypes.map((type: any) => {
const fieldData = type.fieldMapping.map((fields: any) => {
return fields;
});
const fieldMapping: any = fieldData;
if (!isEmpty(fieldMapping)) {
if (search) {
filteredResult = fieldMapping.filter((item: any) =>
item?.otherCmsField?.toLowerCase().includes(search)
);
totalCount = filteredResult.length;
result = filteredResult.slice(skip, Number(skip) + Number(limit));
} else {
totalCount = fieldMapping.length;
result = fieldMapping.slice(skip, Number(skip) + Number(limit));
}
}
}
});
return { count: totalCount, fieldMapping: result };
};

Expand All @@ -170,7 +179,11 @@ const getExistingContentTypes = async (req: Request) => {
token_payload?.region,
token_payload?.user_id
);
const project = await ProjectModel.findById(projectId);
await ProjectModelLowdb.read();
const project = ProjectModelLowdb.chain
.get("projects")
.find({ id: projectId })
.value();
const stackId = project?.destination_stack_id;
const [err, res] = await safePromise(
https({
Expand Down Expand Up @@ -452,8 +465,10 @@ const resetAllContentTypesMapping = async (projectId: string) => {
};
})
);
await ContentTypesMapperModel.bulkWrite(contentTypesbulkWriteOperations, {
ordered: false,

await ContentTypesMapperModelLowdb.read();
ContentTypesMapperModelLowdb.update((data: any) => {
data.ContentTypesMappers.push(contentTypesbulkWriteOperations);
});
return projectDetails;
} catch (error: any) {
Expand Down Expand Up @@ -517,8 +532,10 @@ const removeMapping = async (projectId: string) => {
};
})
);
await ContentTypesMapperModel.bulkWrite(contentTypesbulkWriteOperations, {
ordered: false,

await ContentTypesMapperModelLowdb.read();
ContentTypesMapperModelLowdb.update((data: any) => {
data.ContentTypesMappers.push(contentTypesbulkWriteOperations);
});
projectDetails.content_mapper = [];
await projectDetails?.save();
Expand Down