Skip to content

Commit

Permalink
feat: add save settings feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeocfg committed Oct 23, 2023
1 parent d7a105e commit 621ad91
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/cash.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextFunction, Request, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import format from 'pg-format';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/customers.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NextFunction, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import format from 'pg-format';
import moment from 'moment';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextFunction, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/dict.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextFunction, Request, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/orders.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NextFunction, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import format from 'pg-format';
import moment from 'moment';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/paidOuts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextFunction, Request, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import moment from 'moment';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
49 changes: 47 additions & 2 deletions backend/controllers/settings.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NextFunction, Request, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import { UserRequest } from '../../global';
import { SettingsItem } from '../../frontend/src/models/storeModel';
import format from 'pg-format';

dotenv.config();

Expand Down Expand Up @@ -32,4 +34,47 @@ const getSettings = async (
}
};

export { getSettings };
// @desc set settings
// @route POST /api/settings
// @access Private
const setSettings = async (
request: UserRequest,
response: Response,
next: NextFunction
) => {
try {
const { settings } = request.body;
const currentTimestamp = format('%s', 'NOW()');

const setSettings = await db.query(
`
UPDATE
"${process.env.DB_NAME}"."utils" as u set -- postgres FTW
value = u2.value,
"updatedDate" = u2.updateDate
FROM
(values
${settings
.map((item: SettingsItem) => {
return `(${item.id}, ${item.value}, ${currentTimestamp})`;
})
.join(', ')}
) as u2(id, value, updateDate)
WHERE
u2.id = u.id
RETURNING
*;
`
);

response.json(setSettings.rows);
} catch (error: any) {
console.log(error);
response.status(404).json({
message: error.message,
});
next(`Error: ${error.message}`);
}
};

export { getSettings, setSettings };
2 changes: 1 addition & 1 deletion backend/controllers/tgBot.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NextFunction, Response } from 'express';
import moment from 'moment';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import axios from 'axios';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SHA256 } from 'crypto-js';
import { NextFunction, Request, Response } from 'express';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import generateJwt from '../utils/generateToken';
import getSetString from '../utils/queryBuilders';
import dotenv from 'dotenv';
import format from 'pg-format';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion backend/middleware/authMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { UserRequest } from '../../frontend/src/models/UserRequestModels';
import db from '../config/db';
import dotenv from 'dotenv';
import { UserRequest } from '../../global';

dotenv.config();

Expand Down
3 changes: 2 additions & 1 deletion backend/routes/settings.routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import express from 'express';
import { getSettings } from '../controllers/settings.controller';
import { getSettings, setSettings } from '../controllers/settings.controller';
import { protect } from '../middleware/authMiddleware';

const settingsRoutes = express.Router();

settingsRoutes.route('/').get(protect, getSettings);
settingsRoutes.route('/').post(protect, setSettings);

export default settingsRoutes;
13 changes: 10 additions & 3 deletions frontend/src/pages/administration/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { StoreModel } from '../../../models/storeModel';
import { SET_SETTINGS } from '../../../store/storeConstants/settingsConstants';
import {
getSettings,
saveSettings,
} from '../../../store/actions/settingsActions';

const Settings = () => {
const dispatch = useDispatch();
Expand All @@ -25,13 +29,16 @@ const Settings = () => {
});
};

const save = () => {
console.log('save');
const save = async () => {
await dispatch(saveSettings(settings));
await dispatch(getSettings());
};

return (
<>
<h1>Настройки</h1>
<div className="search-row">
<h1>Настройки</h1>
</div>

<Card>
<CardContent className="settings">
Expand Down
44 changes: 44 additions & 0 deletions frontend/src/store/actions/settingsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import api from '../../utils/axiosMiddleware';
import { SET_SETTINGS } from '../storeConstants/settingsConstants';
import { ADD_NOTIFY } from '../storeConstants/snackbarConstants';
import { setLoader } from './mainActions';
import { SettingsStore } from '../../models/storeModel';

export const getSettings = () => async (dispatch: Dispatch<any>) => {
try {
Expand Down Expand Up @@ -45,3 +46,46 @@ export const getSettings = () => async (dispatch: Dispatch<any>) => {
dispatch(setLoader(false));
}
};

export const saveSettings =
(settings: SettingsStore['settings']) => async (dispatch: Dispatch<any>) => {
try {
dispatch(setLoader(true));

api
.post(`/api/settings`, { settings })
.then((res) => {
dispatch({
type: SET_SETTINGS,
payload: res.data,
});
})
.catch((err) => {
dispatch({
type: ADD_NOTIFY,
payload: {
message: err.response?.data?.message
? err.response.data.message
: 'Ошибка',
type: 'error',
},
});
})
.finally(() => {
dispatch(setLoader(false));
});
} catch (error: any) {
dispatch({
type: ADD_NOTIFY,
payload: {
message:
error.response && error.response.data
? error.response.data.message
: 'Ошибка',
type: 'error',
},
});

dispatch(setLoader(false));
}
};
4 changes: 2 additions & 2 deletions frontend/src/models/UserRequestModels.ts → global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request } from 'express';

export type UserRequest = Request & {
export interface UserRequest extends Request {
user?: {
id?: number;
login?: string;
Expand All @@ -16,4 +16,4 @@ export type UserRequest = Request & {
percentFromParts?: number;
percentFromVisit?: number;
};
};
}
20 changes: 20 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,23 @@ DELETE FROM

DELETE FROM
"service-crm"."cashHistoryLog";

update "service-crm".utils as u set -- postgres FTW
value = u2.value,
"updatedDate" = u2.updateDate
from (values
(1, '12', now()),
(2, '32', now())
) as u2(id, value, updateDate)
where u2.id = u.id;

UPDATE
"service-crm"."utils" as u set -- postgres FTW
value = u2.value,
"updatedDate" = u2.updateDate
FROM
(values
(1, 23, NOW())
) as u2(id, value, updateDate)
WHERE
u2.id = u.id;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["frontend/src", "backend", "models"]
"include": ["frontend/src", "backend", "models", "**/*.d.ts"]
}

0 comments on commit 621ad91

Please sign in to comment.