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
2 changes: 0 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { config } from "./config/index.js";
import logger from "./utils/logger.js";
import ProjectModel from "./models/project.js";
import ContentTypesMapperModel from "./models/contentTypesMapper.js";
import FieldMapperModel from "./models/FieldMapper.js";
import fs from "fs";

const connectToDatabase = async () => {
Expand All @@ -21,7 +20,6 @@ const connectToDatabase = async () => {
// Create the collection's if it doesn't exist
await ProjectModel.init();
await ContentTypesMapperModel.init();
await FieldMapperModel.init();
} catch (error) {
logger.error("Error while connecting to MongoDB:", error);
process.exit(1);
Expand Down
48 changes: 21 additions & 27 deletions src/models/FieldMapper.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
import { Schema, model, Document } from "mongoose";
import { JSONFile } from "lowdb/node";
import LowWithLodash from "../utils/lowdb-lodash.utils.js";

interface FieldMapper extends Document {
uid: string;
otherCmsField: string;
otherCmsType: string;
contentstackField: string;
contentstackFieldUid: string;
ContentstackFieldType: string;
isDeleted: boolean;
backupFieldType: string;
refrenceTo: { uid: string; title: string };
interface FieldMapper {
field_mapper: {
id: string;
uid: string;
otherCmsField: string;
otherCmsType: string;
contentstackField: string;
contentstackFieldUid: string;
ContentstackFieldType: string;
isDeleted: boolean;
backupFieldType: string;
refrenceTo: { uid: string; title: string };
}[];
}

const fieldMapperSchema = new Schema<FieldMapper>({
uid: { type: String, required: true },
otherCmsField: { type: String, required: true },
otherCmsType: { type: String, required: true },
contentstackField: { type: String },
contentstackFieldUid: { type: String },
ContentstackFieldType: { type: String, required: true },
isDeleted: { type: Boolean, default: false },
backupFieldType: { type: String },
refrenceTo: {
uid: { type: String },
title: { type: String },
},
});
const defaultData: FieldMapper = { field_mapper: [] };

const FieldMapperModel = model<FieldMapper>("FieldMapping", fieldMapperSchema);
const db = new LowWithLodash(
new JSONFile<FieldMapper>("database/field-mapper.json"),
defaultData
);

export default FieldMapperModel;
export default db;
138 changes: 70 additions & 68 deletions src/services/contentMapper.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Request } from "express";
import ContentTypesMapperModel from "../models/contentTypesMapper.js";
import FieldMapperModel from "../models/FieldMapper.js";
import ProjectModel from "../models/project.js";
import { getLogMessage, isEmpty, safePromise } from "../utils/index.js";
import {
Expand All @@ -22,31 +21,29 @@ import https from "../utils/https.utils.js";
import getAuthtoken from "../utils/auth.utils.js";
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";

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

// console.log(contentTypes)
await Promise.all(
contentTypes.map(async (type: any, index: any) => {
await FieldMapperModel.insertMany(type.fieldMapping, {
ordered: true,
})
.then(function (docs: any) {
// do something with docs
contentTypes[index].fieldMapping = docs.map((item: any) => {
return item._id;
});
})
.catch(function () {
// console.log("type.fieldMapping")
// console.log(err)
// error handling here
});
})
);
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 };
});

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

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

let typeIds: any = [];

Expand Down Expand Up @@ -279,14 +276,17 @@ const updateContentType = async (req: Request) => {
throw new BadRequestError(HTTP_TEXTS.CONTENT_TYPE_NOT_FOUND);
}
if (!isEmpty(fieldMapping)) {
const bulkWriteOperations = fieldMapping?.map((doc: any) => ({
replaceOne: {
filter: { _id: doc._id },
replacement: doc,
upsert: true,
},
}));
await FieldMapperModel.bulkWrite(bulkWriteOperations, { ordered: false });
await FieldMapperModel.read();
(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;
});
}
});
}

return { updatedContentType };
Expand Down Expand Up @@ -359,21 +359,22 @@ const resetToInitialMapping = async (req: Request) => {

try {
if (!isEmpty(contentType?.fieldMapping)) {
const bulkWriteOperations: any = contentType?.fieldMapping?.map(
(doc: any) => ({
updateOne: {
filter: { _id: doc._id },
update: {
$set: {
contentstackField: "",
contentstackFieldUid: "",
ContentstackFieldType: doc.backupFieldType,
},
},
},
})
);
await FieldMapperModel.bulkWrite(bulkWriteOperations, { ordered: false });
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,
};
});
}
});
}
contentType.contentstackTitle = "";
contentType.contentstackUid = "";
Expand Down Expand Up @@ -421,22 +422,21 @@ const resetAllContentTypesMapping = async (projectId: string) => {
const contentTypesbulkWriteOperations: any = await Promise.all(
contentTypes?.map(async (contentType: any) => {
if (!isEmpty(contentType?.fieldMapping)) {
const bulkWriteOperations: any = contentType?.fieldMapping?.map(
(doc: any) => ({
updateOne: {
filter: { _id: doc._id },
update: {
$set: {
contentstackField: "",
contentstackFieldUid: "",
ContentstackFieldType: doc.backupFieldType,
},
},
},
})
);
await FieldMapperModel.bulkWrite(bulkWriteOperations, {
ordered: false,
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,
};
});
}
});
}
return {
Expand Down Expand Up @@ -494,18 +494,20 @@ const removeMapping = async (projectId: string) => {
try {
const contentTypes = projectDetails?.content_mapper;

//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)) {
const bulkWriteOperations: any = contentType?.fieldMapping?.map(
(doc: any) => ({
deleteOne: {
filter: { _id: doc._id },
},
})
);
await FieldMapperModel.bulkWrite(bulkWriteOperations, {
ordered: false,
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];
});
}
});
}
return {
Expand Down