mirror of
https://github.com/Ravinou/borgwarehouse
synced 2026-03-14 22:35:46 +01:00
refactor: ⚡ create config service
This commit is contained in:
parent
201f5b41a1
commit
9e2ae9f0fa
25 changed files with 31 additions and 86 deletions
|
|
@ -1,55 +0,0 @@
|
|||
import { promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
import { BorgWarehouseUser, Repository } from '~/types/domain/config.types';
|
||||
import repoHistory from './repoHistory';
|
||||
|
||||
// Paths definition
|
||||
const jsonDirectory = path.join(process.cwd(), '/config');
|
||||
const usersFilePath = path.join(jsonDirectory, 'users.json');
|
||||
const repoFilePath = path.join(jsonDirectory, 'repo.json');
|
||||
|
||||
export const getUsersList = async (): Promise<BorgWarehouseUser[]> => {
|
||||
try {
|
||||
const fileContent = await fs.readFile(usersFilePath, 'utf8');
|
||||
return JSON.parse(fileContent) || [];
|
||||
} catch (error) {
|
||||
console.log('Error reading users.json:', error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const updateUsersList = async (usersList: BorgWarehouseUser[]): Promise<void> => {
|
||||
try {
|
||||
await fs.writeFile(usersFilePath, JSON.stringify(usersList, null, 2));
|
||||
} catch (error) {
|
||||
console.log('Error writing users.json:', error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getRepoList = async (): Promise<Repository[]> => {
|
||||
try {
|
||||
await fs.access(repoFilePath);
|
||||
} catch {
|
||||
console.log('repo.json not found, creating a new one.');
|
||||
await fs.writeFile(repoFilePath, JSON.stringify([]));
|
||||
}
|
||||
|
||||
try {
|
||||
const fileContent = await fs.readFile(repoFilePath, 'utf8');
|
||||
return JSON.parse(fileContent) || [];
|
||||
} catch (error) {
|
||||
console.log('Error reading repo.json:', error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const updateRepoList = async (repoList: Repository[], history = false): Promise<void> => {
|
||||
try {
|
||||
if (history) {
|
||||
await repoHistory(repoList);
|
||||
}
|
||||
await fs.writeFile(repoFilePath, JSON.stringify(repoList, null, 2));
|
||||
} catch (error) {
|
||||
console.log('Error writing repo.json:', error);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
export * from './auth';
|
||||
export * from './fileHelpers';
|
||||
export * from './isSshPubKeyDuplicate';
|
||||
export * from './lanCommandOption';
|
||||
export * from './nodemailerSMTP';
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { IncomingHttpHeaders } from 'http2';
|
||||
import { Optional } from '~/types';
|
||||
import { TokenPermissionsType } from '~/types/api/integration.types';
|
||||
import { getUsersList } from './fileHelpers';
|
||||
import { getUsersList } from '~/services';
|
||||
|
||||
export const tokenController = async (
|
||||
headers: IncomingHttpHeaders
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import { ErrorResponse } from '~/types/api/error.types';
|
||||
import { AppriseAlertResponse } from '~/types/api/notification.types';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import { ErrorResponse } from '~/types/api/error.types';
|
||||
import { AppriseModeDTO } from '~/types/api/notification.types';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import { ErrorResponse } from '~/types/api/error.types';
|
||||
import { AppriseServicesDTO } from '~/types/api/notification.types';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
//Lib
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import { ErrorResponse } from '~/types/api/error.types';
|
||||
import { EmailAlertDTO } from '~/types/api/notification.types';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { exec } from 'child_process';
|
|||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { promisify } from 'util';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import { ErrorResponse, SuccessResponse } from '~/types/api/error.types';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// Imports
|
||||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { getServerSession } from 'next-auth/next';
|
|||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { AppriseServicesDTO } from '~/types/api/notification.types';
|
||||
import { ErrorResponse } from '~/types/api/error.types';
|
||||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest & { body: AppriseServicesDTO },
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
import { authOptions } from '../auth/[...nextauth]';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { EmailAlertDTO } from '~/types/api/notification.types';
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import fs from 'fs';
|
|||
import NextAuth, { NextAuthOptions, RequestInternal, User } from 'next-auth';
|
||||
import CredentialsProvider from 'next-auth/providers/credentials';
|
||||
import path from 'path';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import { verifyPassword } from '../../../helpers/functions/auth';
|
||||
|
||||
const logLogin = async (message: string, req: Partial<RequestInternal>, success = false) => {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { NextApiRequest, NextApiResponse } from 'next';
|
|||
import { exec as execCallback } from 'node:child_process';
|
||||
import { promisify } from 'util';
|
||||
import ApiResponse from '~/helpers/functions/apiResponse';
|
||||
import { getRepoList, getUsersList, updateRepoList } from '~/helpers/functions/fileHelpers';
|
||||
import { getRepoList, getUsersList, updateRepoList } from '~/services';
|
||||
import nodemailerSMTP from '~/helpers/functions/nodemailerSMTP';
|
||||
import { getLastSaveListShell } from '~/helpers/functions/shell.utils';
|
||||
import emailAlertStatus from '~/helpers/templates/emailAlertStatus';
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
// dbHelper.ts
|
||||
import { Low } from 'lowdb';
|
||||
import { JSONFile } from 'lowdb/node';
|
||||
import path from 'path';
|
||||
|
|
|
|||
2
services/index.ts
Normal file
2
services/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from './config.service';
|
||||
export * from './history.service';
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
import { createMocks } from 'node-mocks-http';
|
||||
import handler from '~/pages/api/cronjob/checkStatus';
|
||||
import { getRepoList, getUsersList, updateRepoList } from '~/helpers/functions/fileHelpers';
|
||||
import { getRepoList, getUsersList, updateRepoList } from '~/services';
|
||||
import { getLastSaveListShell } from '~/helpers/functions/shell.utils';
|
||||
import nodemailerSMTP from '~/helpers/functions/nodemailerSMTP';
|
||||
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
getRepoList: jest.fn(),
|
||||
getUsersList: jest.fn(),
|
||||
updateRepoList: jest.fn(),
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { getServerSession } from 'next-auth/next';
|
||||
import { createMocks } from 'node-mocks-http';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import handler from '~/pages/api/account/getAppriseAlert';
|
||||
|
||||
jest.mock('next-auth/next');
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
getUsersList: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { getServerSession } from 'next-auth/next';
|
||||
import { createMocks } from 'node-mocks-http';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import handler from '~/pages/api/account/getAppriseMode';
|
||||
|
||||
jest.mock('next-auth/next');
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
getUsersList: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { getServerSession } from 'next-auth/next';
|
||||
import { createMocks } from 'node-mocks-http';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
import handler from '~/pages/api/account/getAppriseServices';
|
||||
|
||||
jest.mock('next-auth/next');
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
getUsersList: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ import handler from '~/pages/api/account/getEmailAlert';
|
|||
import { getServerSession } from 'next-auth/next';
|
||||
import { promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
import { getUsersList } from '~/helpers/functions';
|
||||
import { getUsersList } from '~/services';
|
||||
|
||||
jest.mock('next-auth/next');
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
getUsersList: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { createMocks } from 'node-mocks-http';
|
||||
import handler from '~/pages/api/account/updateAppriseAlert';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
|
||||
jest.mock('next-auth/next');
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
__esModule: true,
|
||||
getUsersList: jest.fn(),
|
||||
updateUsersList: jest.fn(),
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { createMocks } from 'node-mocks-http';
|
||||
import handler from '~/pages/api/account/updateAppriseMode';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
|
||||
jest.mock('next-auth/next');
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
getUsersList: jest.fn(),
|
||||
updateUsersList: jest.fn(),
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import { createMocks } from 'node-mocks-http';
|
||||
import handler from '~/pages/api/account/updateAppriseServices';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { getUsersList, updateUsersList } from '~/helpers/functions/fileHelpers';
|
||||
import { getUsersList, updateUsersList } from '~/services';
|
||||
|
||||
// Mock imports
|
||||
jest.mock('next-auth/next');
|
||||
jest.mock('~/helpers/functions/fileHelpers', () => ({
|
||||
jest.mock('~/services', () => ({
|
||||
getUsersList: jest.fn(),
|
||||
updateUsersList: jest.fn(),
|
||||
}));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue