Skip to content

Commit

Permalink
Merge pull request #17 from Iryna-Pavlyk/origin/users5-cloudinary
Browse files Browse the repository at this point in the history
Origin/users5 cloudinary
  • Loading branch information
Iryna-Pavlyk authored Aug 5, 2024
2 parents e56c1da + 48999e7 commit f946686
Show file tree
Hide file tree
Showing 25 changed files with 309 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ MONGODB_URL=
MONGODB_DB=

CORS_APPROVED_DOMAINS=

CLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
ENABLE_CLOUDINARY=
156 changes: 155 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
},
"dependencies": {
"bcrypt": "^5.1.1",
"cloudinary": "^2.4.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"http-errors": "^2.0.0",
"joi": "^17.13.3",
"mongoose": "^8.5.2",
"multer": "^1.4.5-lts.1",
"pino-http": "^10.2.0",
"swagger-ui-express": "^5.0.1"
}
Expand Down
11 changes: 11 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from 'node:path';

export const TEMP_UPLOAD_DIR = path.resolve('src', 'temp');

export const PUBLIC_DIR = path.resolve('src', 'public');

export const CLOUDINARY = {
CLOUD_NAME: 'CLOUDINARY_CLOUD_NAME',
API_KEY: 'CLOUDINARY_API_KEY',
API_SECRET: 'CLOUDINARY_API_SECRET',
};
File renamed without changes.
2 changes: 2 additions & 0 deletions src/constants/user-constants.js → src/constants/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const emailRegexp = /^[a-zA-Z0-9_.+]+(?<!^[0-9]*)@[a-zA-Z0-9-]+\.[a-zA-Z0
// [0-9a-zA-Z!@#$%^&*]{6,} - строка состоит не менее, чем из 6 вышеупомянутых символов.

export const userGender = ['woman', 'man'];


Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import createHttpError from 'http-errors';
import { signup, findUser } from '../services/auth-services.js';
import { signup, findUser } from '../services/auth.js';
import { compareHash } from '../utils/hash.js';
import {
createSession,
deleteSession,
findSession,
} from '../services/session-services.js';
} from '../services/session.js';

const setupResponseSession = (
res,
{ refreshToken, refreshTokenValidUntil, _id },
) => {
const setupResponseSession = (res, { refreshToken, refreshTokenValidUntil, _id }) => {
res.cookie('refreshToken', refreshToken, {
httpOnly: true,
expires: refreshTokenValidUntil,
Expand Down
24 changes: 21 additions & 3 deletions src/controllers/user-controllers.js → src/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import createHttpError from "http-errors";
import { getUsers, getUserSettings, patchUserSettings } from "../services/users-services.js";
import { getUsers, getUserSettings, patchUserSettings } from "../services/users.js";
import saveFileToPublicDir from "../utils/saveFileToPublicDir.js";
import { saveFileToCloudinary } from "../utils/saveFileToCloudinary.js";
import { env } from "../env.js";

const enable_cloudinary = env('ENABLE_CLOUDINARY');

export const getAllUsersController = async (req, res) => {
const contacts = await getUsers();
Expand All @@ -25,10 +30,23 @@ export const getUserSettingsController = async (req, res) => {
});
};


export const patchUserSettingsController = async (req, res) => {
const userId = req.user._id;
const user = await patchUserSettings(userId, req.body);
let avatar = req.file;
let avatarUrl='';

if (avatar) {
if (enable_cloudinary === 'true') {
avatarUrl = await saveFileToCloudinary(avatar, 'aqautracker');
} else {
avatarUrl = await saveFileToPublicDir(avatar);
}
};

const user = await patchUserSettings(userId, {
...req.body,
avatar:avatarUrl,
});

if (!user) {
throw createHttpError(404, 'User not found');
Expand Down
7 changes: 6 additions & 1 deletion src/db/models/User.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Schema, model } from 'mongoose';
import { mongooseSaveError, setUpdateSettings } from './hooks.js';
import { emailRegexp, userGender } from '../../constants/user-constants.js';
import { emailRegexp, userGender } from '../../constants/user.js';

const userSchema = new Schema({
name: {
Expand Down Expand Up @@ -37,6 +37,11 @@ const userSchema = new Schema({
avatar: {
type: String,
},
userId: {
type: Schema.Types.ObjectId,
ref: "user",
required: true,
}
}, {timestamps: true, versionKey: false});

userSchema.post("save", mongooseSaveError);
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { PUBLIC_DIR, TEMP_UPLOAD_DIR } from './constants/index.js';
import { initMongoConnection } from './db/initMongoConnection.js';
import { setupServer } from './server.js';
import createDirIfNotExists from './utils/createDirIfNotExists.js';

// const message = 'Hello world';
// console.log(message);

const bootstrap = async () => {
await initMongoConnection();
await createDirIfNotExists(TEMP_UPLOAD_DIR);
await createDirIfNotExists(PUBLIC_DIR);
setupServer();
};

Expand Down
Loading

0 comments on commit f946686

Please sign in to comment.