"use strict"; const models = require('./../models/index'); import commonHelper from "../helpers/common"; import { HttpCodes } from "../helpers/responseCodes"; const { writeCsvFile } = new commonHelper(); const csvParser = require('csv-parser'); const fs = require('fs'); const moment = require('moment'); export default class DataMigrateService { constructor() { this.UploadCSV = this.UploadCSV.bind(this); this.UploadCSVGroupMessage = this.UploadCSVGroupMessage.bind(this); this.UploadCSVUsers = this.UploadCSVUsers.bind(this); this.UploadCSVFriendMessage = this.UploadCSVFriendMessage.bind(this); this.UploadCSVFriendGroup = this.UploadCSVFriendGroup.bind(this); this.UploadCSVFriendMaster = this.UploadCSVFriendMaster.bind(this); this.UploadCSVAdvertisement = this.UploadCSVAdvertisement.bind(this); this.UploadCSVAdminuser = this.UploadCSVAdminuser.bind(this); this.UploadCSVSerialnumber = this.UploadCSVSerialnumber.bind(this); this.UploadCSVServicefeature = this.UploadCSVServicefeature.bind(this); this.UploadCSVService = this.UploadCSVService.bind(this); this.UploadCSVPhrasecategory = this.UploadCSVPhrasecategory.bind(this); this.UploadCSVPhrasesubcategory = this.UploadCSVPhrasesubcategory.bind(this); this.UploadCSVPhrase = this.UploadCSVPhrase.bind(this); this.UplodeCSVManageAppVersions = this.UplodeCSVManageAppVersions.bind(this); this.UplodeCSVMymanuDevices = this.UplodeCSVMymanuDevices.bind(this); this.UplodeCSVMymanuDeviceImages = this.UplodeCSVMymanuDeviceImages.bind(this); this.UplodeCSVRole = this.UplodeCSVRole.bind(this); this.UplodeCSVRights = this.UplodeCSVRights.bind(this); this.UplodeCSVRoleRights = this.UplodeCSVRoleRights.bind(this) this.UplodeCSVDeviceRegisterMaster = this.UplodeCSVDeviceRegisterMaster.bind(this); this.SetStartingValue = this.SetStartingValue.bind(this); } //upload csv for group member table to get old php database async UploadCSV(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and insert data into the database readStream .pipe(csvParser()) .on('data', async (row) => { try { const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix(); const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const groupName = row.group_name || ''; const groupId = parseInt(row.group_id); const groupImage = row.group_image || ''; const groupType = row.group_type || 1; const groupqrImage = row.group_qr_image || ''; const ownerId = row.group_owner_id ? parseInt(row.group_owner_id) : null; const memberIds = row.group_member_id ? row.group_member_id.split(',').map((id) => parseInt(id)).filter(id => !isNaN(id)) : []; // Create a new group record in the GroupMaster table const groupRecord = await models.GroupMaster.create({ group_id: groupId, group_name: groupName, group_image: groupImage, group_owner_id: ownerId, group_type: groupType, group_qr_image: groupqrImage, created_date: createdDateTimestamp, updated_date: updatedDateTimestamp, }); const currentGroupId = groupRecord.group_id; // Get the current group's ID if (memberIds.length > 0) { // Create group member records in the GroupMember table for each memberId const groupMemberData = memberIds.map((memberId) => ({ group_id: currentGroupId, // Set the group_id to the current group's ID user_id: memberId, created_date: createdDateTimestamp, updated_date: updatedDateTimestamp, })); // Insert groupMemberData into the database await models.GroupMember.bulkCreate(groupMemberData); } } catch (error) { console.error('Error inserting data:', error); } }) .on('end', () => { fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for group message table to get old php database async UploadCSVGroupMessage(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data const groupId = parseInt(row.group_id); // Check if the group_id exists in the GroupMaster table const groupExists = await models.GroupMaster.findOne({ where: { group_id: groupId }, }); if (groupExists) { // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix(); const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; await models.GroupMessage.create({ group_message_id: parseInt(row.group_message_id), group_id: groupId, sender_id: row.sender_id || null, user_id: row.user_id || null, group_message: row.group_message || '', is_deleted: row.is_deleted || 0, public_deleted_flag: row.public_deleted_flag || 0, deleted_by: row.deleted_by == "NULL" ? null : row.deleted_by, reply_message_id: row.reply_message_id == "NULL" ? null : row.reply_message_id, tags: row.tags ? row.tags : '[]', created_date: createdDateTimestamp, updated_date: updatedDateTimestamp, language_code: 'en-US' }); } else { // The group_id doesn't exist in the GroupMaster table, skip this row console.log(`Group with ID ${groupId} does not exist in GroupMaster. Skipping...`); } } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for user table to get old php database // async UploadCSVUsers(req, callback) { // try { // const createdDateKey = 'created_date'; // const updatedDateKey = 'updated_date'; // const isloginDateKey = 'is_login'; // const isLoginTranslateDateKey = 'is_login_translate'; // const tempFilePath = 'file.csv'; // Replace with the actual path // await writeCsvFile(req.file.buffer, tempFilePath); // // Read from the temporary file instead of the buffer // const readStream = fs.createReadStream(tempFilePath); // const messagesToInsert = []; // Collect messages to insert in bulk // // Read the CSV file and collect data for bulk insertion // readStream // .pipe(csvParser()) // .on('data', async (row) => { // try { // // Extract the group_id from your CSV data // // The group_id exists in the GroupMaster table, proceed with message insertion // const createdDateString = row[createdDateKey]; // const updatedDateString = row[updatedDateKey]; // const isloginDateString = row[isloginDateKey]; // const isLoginTranslateDateString = row[isLoginTranslateDateKey]; // const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; // const updatedDateTimestamp = updatedDateString // ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() // : null; // const isloginDateTimestamp = isloginDateString ? moment(isloginDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; // const isLoginTranslateDateDateTimestamp = isLoginTranslateDateString // ? moment(isLoginTranslateDateString, 'YYYY-MM-DD HH:mm:ss').unix() // : null; // await models.User.create({ // user_id: parseInt(row.user_id), // user_name: row.user_name, // user_email: row.user_email, // user_password: row.user_password, // profile_picture: row.profile_picture == "NULL" ? '' : row.profile_picture, // user_login_type: row.user_login_type == "NULL" ? 0 : parseInt(row.user_login_type), // title: row.title == "NULL" ? '' : row.title, // registered_from: row.registered_from == "NULL" ? '' : row.registered_from, // facebook_id: row.facebook_id == "NULL" ? '' : row.facebook_id, // google_plus_id: row.google_plus_id == "NULL" ? '' : row.google_plus_id, // twitter_id: row.twitter_id == "NULL" ? '' : row.twitter_id, // activation_key: row.activation_key == "NULL" ? '' : row.activation_key, // is_verified: row.is_verified == "NULL" ? 0 : parseInt(row.is_verified), // is_translate_app: row.is_translate_app == "NULL" ? 0 : parseInt(row.is_translate_app), // is_mymanuplay_app: row.is_mymanuplay_app == "NULL" ? 0 : parseInt(row.is_mymanuplay_app), // push_notification: row.push_notification == "NULL" ? 1 : parseInt(row.push_notification), // device_token: row.device_token == "NULL" ? '' : row.device_token, // device_token_translate: row.device_token_translate == "NULL" ? '' : row.device_token_translate, // device_type: row.device_type == "NULL" ? 0 : parseInt(row.device_type), // device_type_translate: row.device_type_translate == "NULL" ? 0 : parseInt(row.device_type_translate), // is_deleted: row.is_deleted == "NULL" ? 1 : parseInt(row.is_deleted), // status: row.status == "NULL" ? 1 : parseInt(row.status), // is_login: isNaN(isloginDateTimestamp) ? null : isloginDateTimestamp, // is_login_translate: isNaN(isLoginTranslateDateDateTimestamp) ? null : isLoginTranslateDateDateTimestamp, // login_access_token: '', // login_access_token_translate: '', // is_post_activity: row.is_post_activity == "NULL" ? 0 : parseInt(row.is_post_activity), // block_for_chat: row.block_for_chat == "NULL" ? 1 : parseInt(row.block_for_chat), // language_code: 'en-US', // region: row.region == "NULL" ? '' : row.region, // user_qr_image: row.user_qr_image == "NULL" ? '' : row.user_qr_image, // subscription_plan: row.subscription_plan == "NULL" ? '' : row.subscription_plan, // subscription_plan_device_type: row.subscription_plan_device_type == "NULL" ? '' : row.subscription_plan_device_type, // subscribed_on: row.subscribed_on == "NULL" ? null : parseInt(row.subscribed_on), // voice_preference: row.voice_preference == "NULL" ? '' : row.voice_preference, // created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, // updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, // is_deleted_translate: row.is_deleted_translate == "NULL" ? null : parseInt(row.is_deleted_translate), // otp: row.otp ? row.otp : null, // }); // } catch (error) { // console.error('Error collecting data:', error); // } // }) // .on('end', async () => { // try { // // Bulk insert messages only if there are messages to insert // fs.unlinkSync(tempFilePath); // return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); // } catch (err) { // console.error('Error inserting data:', err); // return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); // } // }); // } catch (error) { // console.log(error.message, 'removeFriendMessage'); // return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); // } // } async UploadCSVUsers(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const isloginDateKey = 'is_login'; const isLoginTranslateDateKey = 'is_login_translate'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); const batchInsert = async (data) => { await models.User.bulkCreate(data); }; const messagesToInsert = []; // Collect messages to insert in bulk // Read the CSV file and collect data for bulk insertion readStream.pipe(csvParser()) .on('data', (row) => { try { // Extract date values const createdDateTimestamp = row[createdDateKey] ? moment(row[createdDateKey], 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = row[updatedDateKey] ? moment(row[updatedDateKey], 'YYYY-MM-DD HH:mm:ss').unix() : null; const isloginDateTimestamp = row[isloginDateKey] ? moment(row[isloginDateKey], 'YYYY-MM-DD HH:mm:ss').unix() : null; const isLoginTranslateDateDateTimestamp = row[isLoginTranslateDateKey] ? moment(row[isLoginTranslateDateKey], 'YYYY-MM-DD HH:mm:ss').unix() : null; // Prepare data for insertion messagesToInsert.push({ user_id: parseInt(row.user_id), user_name: row.user_name, user_email: row.user_email, user_password: row.user_password, profile_picture: row.profile_picture === "NULL" ? '' : row.profile_picture, user_login_type: row.user_login_type === "NULL" ? 0 : parseInt(row.user_login_type), title: row.title === "NULL" ? '' : row.title, registered_from: row.registered_from === "NULL" ? '' : row.registered_from, facebook_id: row.facebook_id === "NULL" ? '' : row.facebook_id, google_plus_id: row.google_plus_id === "NULL" ? '' : row.google_plus_id, twitter_id: row.twitter_id === "NULL" ? '' : row.twitter_id, activation_key: row.activation_key === "NULL" ? '' : row.activation_key, is_verified: row.is_verified === "NULL" ? 0 : parseInt(row.is_verified), is_translate_app: row.is_translate_app === "NULL" ? 0 : parseInt(row.is_translate_app), is_mymanuplay_app: row.is_mymanuplay_app === "NULL" ? 0 : parseInt(row.is_mymanuplay_app), push_notification: row.push_notification === "NULL" ? 1 : parseInt(row.push_notification), device_token: row.device_token === "NULL" ? '' : row.device_token, device_token_translate: row.device_token_translate === "NULL" ? '' : row.device_token_translate, device_type: row.device_type === "NULL" ? 0 : parseInt(row.device_type), device_type_translate: row.device_type_translate === "NULL" ? 0 : parseInt(row.device_type_translate), is_deleted: row.is_deleted === "NULL" ? 1 : parseInt(row.is_deleted), status: row.status === "NULL" ? 1 : parseInt(row.status), is_login: isNaN(isloginDateTimestamp) ? null : isloginDateTimestamp, is_login_translate: isNaN(isLoginTranslateDateDateTimestamp) ? null : isLoginTranslateDateDateTimestamp, login_access_token: '', login_access_token_translate: '', is_post_activity: row.is_post_activity === "NULL" ? 0 : parseInt(row.is_post_activity), block_for_chat: row.block_for_chat === "NULL" ? 1 : parseInt(row.block_for_chat), language_code: 'en-US', region: row.region === "NULL" ? '' : row.region, user_qr_image: row.user_qr_image === "NULL" ? '' : row.user_qr_image, subscription_plan: row.subscription_plan === "NULL" ? '' : row.subscription_plan, subscription_plan_device_type: row.subscription_plan_device_type === "NULL" ? '' : row.subscription_plan_device_type, subscribed_on: row.subscribed_on === "NULL" ? null : parseInt(row.subscribed_on), voice_preference: row.voice_preference === "NULL" ? '' : row.voice_preference, created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, is_deleted_translate: row.is_deleted_translate === "NULL" ? null : parseInt(row.is_deleted_translate), otp: row.otp || null, }); // Insert data in batches to avoid overwhelming the database if (messagesToInsert.length >= 1000) { batchInsert(messagesToInsert.slice()); // Create a copy to prevent mutation messagesToInsert.length = 0; // Clear the array } } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Insert any remaining data if (messagesToInsert.length > 0) { await batchInsert(messagesToInsert); } // Clean up temporary file fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [] }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [] }); } }); } catch (error) { console.error(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [] }); } } //upload csv for friend message table to get old php database async UploadCSVFriendMessage(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const isloginDateKey = 'is_login'; const isLoginTranslateDateKey = 'is_login_translate'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const friendMessageEntry = await models.FriendGroup.findOne({ where: { friend_group_id: parseInt(row.friend_group_id), }, }); // Check if a valid friend_message entry was found. if (friendMessageEntry) { // Combination exists, proceed with FriendMessage creation.} await models.FriendMessage.create({ friend_message_id: parseInt(row.friend_message_id), friend_group_id: parseInt(row.friend_group_id), sender_id: parseInt(row.sender_id), receiver_id: parseInt(row.receiver_id), message: row.message ? row.message : '', sender_deleted: parseInt(row.sender_deleted) ? parseInt(row.sender_deleted) : 0, receiver_deleted: parseInt(row.receiver_deleted) ? parseInt(row.receiver_deleted) : 0, reply_message_id: parseInt(row.reply_message_id) ? parseInt(row.reply_message_id) : 0, created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, language_code: 'en-US', receiver_message: row.message ? row.message : '', receiver_language_code: 'en-US', }); } else { // Combination doesn't exist, skip FriendMessage creation. console.log("Combination not found in friend_message. Skipping FriendMessage creation.", row.friend_group_id); } } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for friend group table to get old php database async UploadCSVFriendGroup(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const friendMasterEntry = await models.FriendMaster.findOne({ where: { invite_from_id: parseInt(row.owner_id), invite_to_id: parseInt(row.member_id), is_friend: 1, }, }); if (friendMasterEntry) { let data = await models.FriendGroup.create({ friend_group_id: parseInt(row.friend_group_id), friend_group_name: row.friend_group_name, owner_id: parseInt(row.owner_id), member_id: parseInt(row.member_id), created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } else { // Combination doesn't exist or is_friend is not 1, skip FriendGroup creation. console.log("Combination not found in friend_master or is_friend is not 1. Skipping FriendGroup creation.", row.owner_id, row.member_id, "friend_group_id", row.friend_group_id); } } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for friend master table to get old php database async UploadCSVFriendMaster(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; let data = await models.FriendMaster.create({ invite_id: parseInt(row.invite_id), invite_from_id: parseInt(row.invite_from_id) || null, invite_to_id: parseInt(row.invite_to_id) || null, is_invite_sent: parseInt(row.is_invite_sent) || 0, is_friend: parseInt(row.is_friend) || 0, created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for advertisement table to get old php database async UploadCSVAdvertisement(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; let data = await models.AdvertisementMaster.create({ advertisement_id: parseInt(row.advertisement_id), advertisement_image: row.advertisement_image || '', advertisement_link: row.advertisement_link || '', sort_order: parseInt(row.sort_order) || null, status: parseInt(row.status) || 1, is_discover: parseInt(row.is_discover) || 1, title: row.title || '', description: row.description || '', created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for admin user table to get old php database async UploadCSVAdminuser(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; let data = await models.Admin.create({ admin_id: parseInt(row.admin_id), fullname: row.fullname || '', role_id: parseInt(row.role_id) || null, emailid: row.emailid || '', password: row.password || '', status: parseInt(row.status) || 1, login_access_token: '', otp: row.otp ? row.otp : null, created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for serial number table to get old php database async UploadCSVSerialnumber(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; let data = await models.SerialNumber.create({ id: parseInt(row.id), serial_number: row.serial_number || '', device_type: row.device_type || '', created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for service feature table to get old php database async UploadCSVServicefeature(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; let data = await models.ServiceFeature.create({ feature_id: parseInt(row.feature_id), feature: row.feature || '', created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for service table to get old php database async UploadCSVService(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString ? moment(createdDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; const updatedDateTimestamp = updatedDateString ? moment(updatedDateString, 'YYYY-MM-DD HH:mm:ss').unix() : null; let data = await models.Services.create({ id: parseInt(row.id), service_title: row.service_title || '', service_image: row.service_image || '', service_price: row.service_price || '', service_short_description: row.service_short_description || '', service_description: row.service_description || '', service_status: parseInt(row.service_status) || 1, created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); const currentServiceId = data.id; // Get the current group's ID const featureIds = row.feature_id ? row.feature_id.split(',').map((id) => parseInt(id)).filter(id => !isNaN(id)) : []; if (featureIds.length > 0) { // Create group member records in the ServiceFeatureData table for each featureId const ServiceFeatureData = featureIds.map((featureId) => ({ service_id: currentServiceId, // Set the service_id to the current service's ID feature_id: featureId, created_date: createdDateTimestamp, updated_date: updatedDateTimestamp, })); // Insert ServiceFeatureData into the database await models.ServiceFeatureData.bulkCreate(ServiceFeatureData); } else { // Handle the case where there are no valid featureIds (e.g., log a message or take appropriate action) console.warn('No valid featureIds found for this service.'); } } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for phrase category table to get old php database async UploadCSVPhrasecategory(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString const updatedDateTimestamp = updatedDateString let data = await models.PhrasesCategory.create({ phrase_cat_id: parseInt(row.phrase_cat_id), phrase_category: row.phrase_category || '', phrase_image: row.phrase_image || '', phrase_category_am: row.phrase_category_am || '', phrase_category_ar: row.phrase_category_ar || '', phrase_category_bg: row.phrase_category_bg || '', phrase_category_ca: row.phrase_category_ca || '', phrase_category_cs: row.phrase_category_cs || '', phrase_category_da: row.phrase_category_da || '', phrase_category_de: row.phrase_category_de || '', phrase_category_el: row.phrase_category_el || '', phrase_category_en: row.phrase_category_en || '', phrase_category_es: row.phrase_category_es || '', phrase_category_fa: row.phrase_category_fa || '', phrase_category_fi: row.phrase_category_fi || '', phrase_category_fr: row.phrase_category_fr || '', phrase_category_ga: row.phrase_category_ga || '', phrase_category_ha: row.phrase_category_ha || '', phrase_category_he: row.phrase_category_he || '', phrase_category_hu: row.phrase_category_hu || '', phrase_category_id: row.phrase_category_id || '', phrase_category_ig: row.phrase_category_ig || '', phrase_category_it: row.phrase_category_it || '', phrase_category_ja: row.phrase_category_ja || '', phrase_category_ko: row.phrase_category_ko || '', phrase_category_ku: row.phrase_category_ku || '', phrase_category_lt: row.phrase_category_lt || '', phrase_category_lv: row.phrase_category_lv || '', phrase_category_ms: row.phrase_category_ms || '', phrase_category_nb: row.phrase_category_nb || '', phrase_category_nl: row.phrase_category_nl || '', phrase_category_pa: row.phrase_category_pa || '', phrase_category_pl: row.phrase_category_pl || '', phrase_category_prs: row.phrase_category_prs || '', phrase_category_ps: row.phrase_category_ps || '', phrase_category_pt: row.phrase_category_pt || '', phrase_category_ro: row.phrase_category_ro || '', phrase_category_ru: row.phrase_category_ru || '', phrase_category_sk: row.phrase_category_sk || '', phrase_category_sq: row.phrase_category_sq || '', phrase_category_sv: row.phrase_category_sv || '', phrase_category_sw: row.phrase_category_sw || '', phrase_category_th: row.phrase_category_th || '', phrase_category_ti: row.phrase_category_ti || '', phrase_category_tr: row.phrase_category_tr || '', phrase_category_uk: row.phrase_category_uk || '', phrase_category_ur: row.phrase_category_ur || '', phrase_category_vi: row.phrase_category_vi || '', phrase_category_yo: row.phrase_category_yo || '', phrase_category_zh: row.phrase_category_zh || '', created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for phrase sub category table to get old php database async UploadCSVPhrasesubcategory(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString const updatedDateTimestamp = updatedDateString let data = await models.PhrasesSubCategory.create({ phrase_sub_cat_id: parseInt(row.phrase_sub_cat_id), phrase_cat_id: parseInt(row.phrase_cat_id) || null, sub_category: row.sub_category || '', sub_category_am: row.sub_category_am || '', sub_category_ar: row.sub_category_ar || '', sub_category_bg: row.sub_category_bg || '', sub_category_ca: row.sub_category_ca || '', sub_category_cs: row.sub_category_cs || '', sub_category_da: row.sub_category_da || '', sub_category_de: row.sub_category_de || '', sub_category_el: row.sub_category_el || '', sub_category_en: row.sub_category_en || '', sub_category_es: row.sub_category_es || '', sub_category_fa: row.sub_category_fa || '', sub_category_fi: row.sub_category_fi || '', sub_category_fr: row.sub_category_fr || '', sub_category_ga: row.sub_category_ga || '', sub_category_ha: row.sub_category_ha || '', sub_category_he: row.sub_category_he || '', sub_category_hu: row.sub_category_hu || '', sub_category_id: row.sub_category_id || '', sub_category_ig: row.sub_category_ig || '', sub_category_it: row.sub_category_it || '', sub_category_ja: row.sub_category_ja || '', sub_category_ko: row.sub_category_ko || '', sub_category_ku: row.sub_category_ku || '', sub_category_lt: row.sub_category_lt || '', sub_category_lv: row.sub_category_lv || '', sub_category_ms: row.sub_category_ms || '', sub_category_nb: row.sub_category_nb || '', sub_category_nl: row.sub_category_nl || '', sub_category_pa: row.sub_category_pa || '', sub_category_pl: row.sub_category_pl || '', sub_category_prs: row.sub_category_prs || '', sub_category_ps: row.sub_category_ps || '', sub_category_pt: row.sub_category_pt || '', sub_category_ro: row.sub_category_ro || '', sub_category_ru: row.sub_category_ru || '', sub_category_sk: row.sub_category_sk || '', sub_category_sq: row.sub_category_sq || '', sub_category_sv: row.sub_category_sv || '', sub_category_sw: row.sub_category_sw || '', sub_category_th: row.sub_category_th || '', sub_category_ti: row.sub_category_ti || '', sub_category_tr: row.sub_category_tr || '', sub_category_uk: row.sub_category_uk || '', sub_category_ur: row.sub_category_ur || '', sub_category_vi: row.sub_category_vi || '', sub_category_yo: row.sub_category_yo || '', sub_category_zh: row.sub_category_zh || '', created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //upload csv for phrase table to get old php database async UploadCSVPhrase(req, callback) { try { const createdDateKey = 'created_date'; const updatedDateKey = 'updated_date'; const tempFilePath = 'file.csv'; // Replace with the actual path await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { // Extract the group_id from your CSV data // The group_id exists in the GroupMaster table, proceed with message insertion const createdDateString = row[createdDateKey]; const updatedDateString = row[updatedDateKey]; const createdDateTimestamp = createdDateString const updatedDateTimestamp = updatedDateString let data = await models.Phrases.create({ phrase_id: parseInt(row.phrase_id), phrase_cat_id: parseInt(row.phrase_cat_id), phrase_sub_cat_id: parseInt(row.phrase_sub_cat_id), phrase: row.phrase || '', phrases_am: row.phrases_am || '', phrases_ar: row.phrases_ar || '', phrases_bg: row.phrases_bg || '', phrases_ca: row.phrases_ca || '', phrases_cs: row.phrases_cs || '', phrases_da: row.phrases_da || '', phrases_de: row.phrases_de || '', phrases_el: row.phrases_el || '', phrases_en: row.phrases_en || '', phrases_es: row.phrases_es || '', phrases_fa: row.phrases_fa || '', phrases_fi: row.phrases_fi || '', phrases_fr: row.phrases_fr || '', phrases_ga: row.phrases_ga || '', phrases_ha: row.phrases_ha || '', phrases_he: row.phrases_he || '', phrases_hu: row.phrases_hu || '', phrases_id: row.phrases_id || '', phrases_ig: row.phrases_ig || '', phrases_it: row.phrases_it || '', phrases_ja: row.phrases_ja || '', phrases_ko: row.phrases_ko || '', phrases_ku: row.phrases_ku || '', phrases_lt: row.phrases_lt || '', phrases_lv: row.phrases_lv || '', phrases_ms: row.phrases_ms || '', phrases_nb: row.phrases_nb || '', phrases_nl: row.phrases_nl || '', phrases_pa: row.phrases_pa || '', phrases_pl: row.phrases_pl || '', phrases_prs: row.phrases_prs || '', phrases_ps: row.phrases_ps || '', phrases_pt: row.phrases_pt || '', phrases_ro: row.phrases_ro || '', phrases_ru: row.phrases_ru || '', phrases_sk: row.phrases_sk || '', phrases_sq: row.phrases_sq || '', phrases_sv: row.phrases_sv || '', phrases_sw: row.phrases_sw || '', phrases_th: row.phrases_th || '', phrases_ti: row.phrases_ti || '', phrases_tr: row.phrases_tr || '', phrases_uk: row.phrases_uk || '', phrases_ur: row.phrases_ur || '', phrases_vi: row.phrases_vi || '', phrases_yo: row.phrases_yo || '', phrases_zh: row.phrases_zh || '', created_date: isNaN(createdDateTimestamp) ? null : createdDateTimestamp, updated_date: isNaN(updatedDateTimestamp) ? null : updatedDateTimestamp, }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } // uplode csv for manage_app_versions table to get old database async UplodeCSVManageAppVersions(req, callback) { try { const tempFilePath = 'file.csv'; await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { let data = await models.ManageAppVersions.create({ app_version_id: parseInt(row.app_version_id), app_name: row.app_name || '', store_type: row.store_type || 0, application_type: row.application_type || 0, current_version: row.current_version || '', res_msg: row.res_msg || '', created_date: row.created_date, updated_date: row.updated_date || null }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'ManageAppVersionError'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } // uplode csv for MyManu Devices table to get old database async UplodeCSVMymanuDevices(req, callback) { try { const tempFilePath = 'file.csv'; await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { let data = await models.MymanuDevices.create({ mymanu_device_id: parseInt(row.mymanu_device_id), name: row.name || '', description: row.description || '', price: row.price || 0, serial_number: row.serial_number || '', redirect_url: row.redirect_url || '', created_date: row.created_date, updated_date: row.updated_date || null }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'ManageAppVersionError'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } // uplode csv for MyManu Device Images table to get old database async UplodeCSVMymanuDeviceImages(req, callback) { try { const tempFilePath = 'file.csv'; await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { let data = await models.MymanuDeviceImages.create({ mymanu_device_image_id: parseInt(row.mymanu_device_image_id), mymanu_device_id: row.mymanu_device_id || 0, image: row.image || '', is_main_image: row.is_main_image || 0, created_date: row.created_date, updated_date: row.updated_date || null }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'MymanuDeviceImages'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } // uplode csv for Roles table to get old database async UplodeCSVRole(req, callback) { try { const tempFilePath = 'file.csv'; await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { let data = await models.Role.create({ role_id: parseInt(row.role_id), role_name: row.role_name || '', rights: row.rights || '', status: row.status || 0, created_date: row.created_date, updated_date: row.updated_date || null }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'Role'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } // uplode csv for Rights table to get old database async UplodeCSVRights(req, callback) { try { const tempFilePath = 'file.csv'; await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { let data = await models.Rights.create({ rights_id: parseInt(row.rights_id), rights_name: row.rights_name || '', rights_code: row.rights_code || 0, parent_no: row.parent_no || 0, status: row.status || 0, created_date: row.created_date, updated_date: row.updated_date || null }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'Rights'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } // uplode csv for RoleRights table to get old database async UplodeCSVRoleRights(req, callback) { try { const tempFilePath = 'file.csv'; await writeCsvFile(req.file.buffer, tempFilePath); // Read from the temporary file instead of the buffer const readStream = fs.createReadStream(tempFilePath); // Read the CSV file and collect data for bulk insertion readStream .pipe(csvParser()) .on('data', async (row) => { try { let data = await models.RoleRightsData.create({ id: parseInt(row.id), role_id: row.role_id || 0, right_id: row.right_id || 0, created_date: row.created_date, updated_date: row.updated_date || null }); } catch (error) { console.error('Error collecting data:', error); } }) .on('end', async () => { try { // Bulk insert messages only if there are messages to insert fs.unlinkSync(tempFilePath); return callback(null, { status: HttpCodes['API_SUCCESS'], msg: 'DataUploadSuccess', code: HttpCodes['OK'], data: [], }); } catch (err) { console.error('Error inserting data:', err); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } }); } catch (error) { console.log(error.message, 'removeFriendMessage'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } //To accommodate existing data in the PostgreSQL table, set initial values for new entries async SetStartingValue(req, callback) { try { const tablesToSetStartingValue = [ { modelName: 'Admin', columnName: 'admin_id' }, { modelName: 'AdvertisementMaster', columnName: 'advertisement_id' }, { modelName: 'User', columnName: 'user_id' }, { modelName: 'FriendGroup', columnName: 'friend_group_id' }, { modelName: 'FriendMaster', columnName: 'invite_id' }, { modelName: 'FriendMessage', columnName: 'friend_message_id' }, { modelName: 'GroupMaster', columnName: 'group_id' }, { modelName: 'GroupMember', columnName: 'id' }, { modelName: 'GroupMessage', columnName: 'group_message_id' }, { modelName: 'SerialNumber', columnName: 'id' }, { modelName: 'ServiceFeature', columnName: 'feature_id' }, { modelName: 'Services', columnName: 'id' }, { modelName: 'ServiceFeatureData', columnName: 'id' }, { modelName: 'PhrasesCategory', columnName: 'phrase_cat_id' }, { modelName: 'PhrasesSubCategory', columnName: 'phrase_sub_cat_id' }, { modelName: 'Phrases', columnName: 'phrase_id' }, { modelName: 'ManageAppVersions', columnName: 'app_version_id' }, { modelName: 'MymanuDevices', columnName: 'mymanu_device_id' }, { modelName: 'MymanuDeviceImages', columnName: 'mymanu_device_image_id' } ]; let allSequencesSet = true; // Assume all sequences will be set for (const table of tablesToSetStartingValue) { const { modelName, columnName } = table; if (!modelName || !columnName) { return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'BothDataRequired.', code: HttpCodes['BAD_REQUEST'], data: [], }); } const Model = models[modelName]; // Replace `sequelize` with your Sequelize instance if (!Model) { throw new Error(`Model ${modelName} not found.`); } const lastRecord = await Model.findOne({ order: [[columnName, 'DESC']], // Use the specified columnName for ordering }); let nextValue; if (lastRecord) { // Calculate the next value for the sequence nextValue = lastRecord[columnName] + 1; const tableName = Model.getTableName(); const sequenceName = `${tableName}_${columnName}_seq`; // Update the sequence await Model.sequelize.query( `ALTER SEQUENCE "${sequenceName}" RESTART WITH ${nextValue};` ); console.log(`Auto-increment sequence for ${tableName} set to start from ${nextValue}`); } else { nextValue = 1; allSequencesSet = false; // Not all sequences were set console.error('No records found in the table.'); } } if (allSequencesSet) { return callback(null, { status: HttpCodes['API_SUCCESS'], msg: `sequencesetsuccess`, code: HttpCodes['OK'], data: [], }); } else { return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'sequencesetfailure', code: HttpCodes['BAD_REQUEST'], data: [], }); } } catch (error) { console.log(error.message, 'set start value'); return callback(null, { status: HttpCodes['API_FAILURE'], msg: 'SomeThingWentWrong', code: HttpCodes['BAD_REQUEST'], data: [], }); } } }