import { HttpCodes } from '../../config/Errors';
import { db } from '../../settings';
import { ResultSetHeader } from 'mysql2';
import { ErrorResponseC, SuccessResponseC } from '../services.response';
import courseLogs, { ICourseLogs, courseLogger } from './course.logs';
import { formatString } from '../../utils/Strings';
export class DocumentServices {
static insertDocuments = async (
documents: DocumentI[]
): Promise<ResponseT> => {
try {
const sqlInsertQuery =
'INSERT INTO documents (chapter_id, title, url, position) VALUES ?';
const values = documents.map((document) => [
document.chapter_id,
document.title,
document.url,
document.position,
]);
const [result]: any = await db.query<ResultSetHeader[]>(sqlInsertQuery, [
values,
]);
const documentIds = documents.map(
(_, position) => result.insertId + position
);
const resp: ICode<ICourseLogs> = courseLogs.INSERT_DOCUMENTS_SUCCESS;
const msg = formatString(resp.message, {
documentIds: documentIds.join(', '),
});
courseLogger.info(msg, { type: resp.type });
return new SuccessResponseC(resp.type, {}, msg, HttpCodes.Created.code);
} catch (err) {
const msg = formatString(courseLogs.DOCUMENT_ERROR_GENERIC.message, {
error: (err as Error)?.message || '',
});
courseLogger.error(msg, err as Error);
return new ErrorResponseC(
courseLogs.DOCUMENT_ERROR_GENERIC.type,
HttpCodes.InternalServerError.code,
msg
);
}
};
static updateDocuments = async (
documents: DocumentI[]
): Promise<ResponseT> => {
try {
const sqlUpdateQuery =
'UPDATE documents SET title = ?, url = ? WHERE document_id = ?';
const values: any = documents.map((document) => [
document.title,
document.url,
document.document_id,
]);
for (const value of values) {
// const [result]: any =
await db.query<ResultSetHeader>(sqlUpdateQuery, [
value.title,
value.url,
value.document_id,
]);
// if (result.affectedRows === 0) {
// const msg = formatString(courseLogs.DOCUMENT_ERROR_NOT_FOUND.message, {
// documentId: documents.map((document) => document.document_id).join(', '),
// });
// courseLogger.error(msg);
// return new ErrorResponseC(
// courseLogs.DOCUMENT_ERROR_NOT_FOUND.type,
// HttpCodes.NotFound.code,
// msg
// );
// }
}
const resp: ICode<ICourseLogs> = courseLogs.UPDATE_DOCUMENTS_SUCCESS;
const msg = formatString(resp.message, {
documentIds: documents
.map((document) => document.document_id)
.join(', '),
});
courseLogger.info(msg, { type: resp.type });
return new SuccessResponseC(resp.type, {}, msg, HttpCodes.Accepted.code);
} catch (err) {
const msg = formatString(courseLogs.DOCUMENT_ERROR_GENERIC.message, {
error: (err as Error)?.message || '',
});
courseLogger.error(msg, err as Error);
return new ErrorResponseC(
courseLogs.DOCUMENT_ERROR_GENERIC.type,
HttpCodes.InternalServerError.code,
msg
);
}
};
static deleteDocuments = async (
document_ids: number[]
): Promise<ResponseT> => {
try {
const sqlDeleteQuery = 'DELETE FROM documents WHERE document_id IN (?)';
const [result]: any = await db.query<ResultSetHeader[]>(sqlDeleteQuery, [
document_ids,
]);
if (result.affectedRows === 0) {
const msg = formatString(courseLogs.DOCUMENT_ERROR_NOT_FOUND.message, {
documentId: document_ids.join(', '),
});
courseLogger.error(msg);
return new ErrorResponseC(
courseLogs.DOCUMENT_ERROR_NOT_FOUND.type,
HttpCodes.NotFound.code,
msg
);
}
const resp: ICode<ICourseLogs> = courseLogs.DELETE_DOCUMENTS_SUCCESS;
const msg = formatString(resp.message, {
documentIds: document_ids.join(', '),
});
courseLogger.info(msg, { type: resp.type });
return new SuccessResponseC(resp.type, {}, msg, HttpCodes.Accepted.code);
} catch (err) {
const msg = formatString(courseLogs.DOCUMENT_ERROR_GENERIC.message, {
error: (err as Error)?.message || '',
});
courseLogger.error(msg, err as Error);
return new ErrorResponseC(
courseLogs.DOCUMENT_ERROR_GENERIC.type,
HttpCodes.InternalServerError.code,
msg
);
}
};
static identifyAddedDocuments = (
oldDocuments: DocumentI[],
newDocuments: DocumentI[]
) => {
const addedDocuments = newDocuments.filter(
(newDocument) =>
!oldDocuments.some(
(oldDocument) => oldDocument.document_id === newDocument.document_id
)
);
return addedDocuments;
};
static identifyDeletedDocuments = (
oldDocuments: DocumentI[],
newDocuments: DocumentI[]
) => {
const deletedDocuments = oldDocuments.filter(
(oldDocument) =>
!newDocuments.some(
(newDocument) => newDocument.document_id === oldDocument.document_id
)
);
return deletedDocuments;
};
static identifyUpdatedDocuments = (
oldDocuments: DocumentI[],
newDocuments: DocumentI[]
) => {
const updatedDocuments = newDocuments.filter((newDocument) =>
oldDocuments.some(
(oldDocument) =>
oldDocument.document_id === newDocument.document_id &&
(oldDocument.title !== newDocument.title ||
oldDocument.position !== newDocument.position ||
oldDocument.url !== newDocument.url)
)
);
return updatedDocuments;
};
}