Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 88 additions & 6 deletions 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 @@ -41,6 +41,7 @@
"graphql-subscriptions": "^3.0.0",
"graphql-ws": "^6.0.6",
"jose": "^5.10.0",
"multer": "^2.0.2",
"nodemailer": "^7.0.10",
"pg": "^8.14.1",
"reflect-metadata": "^0.2.2",
Expand All @@ -57,6 +58,7 @@
"@types/argon2": "^0.14.1",
"@types/cookies": "^0.9.0",
"@types/jest": "^29.5.14",
"@types/multer": "^2.0.0",
"@types/node": "^22.18.13",
"@types/nodemailer": "^7.0.3",
"@types/uuid": "^10.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/entities/Manager.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export default class ManagerEntity {
})
password: string;

@Column()
profileImage: string;

@Column({ nullable: true})
resetToken?: string

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { authContext } from "./lib/authContext";
import type { Loaders } from "./lib/dataLoaderContext";
import nodemailer from "nodemailer";
import { sendMail } from "./lib/mail";
import uploadImage from "./routes/uploadImage";

export interface MyContext {
req: Request;
Expand All @@ -28,6 +29,9 @@ export interface MyContext {
}

const app = express();

uploadImage(app)

const httpServer = http.createServer(app);

const authorizedCorsUrls = [
Expand Down
50 changes: 50 additions & 0 deletions src/routes/uploadImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import cors from "cors";
import multer from "multer";
import path from "path";
import fs from "fs";
import { Express, Request, Response } from "express";
import ManagerService from "@/services/manager.service";

export default function uploadImage(app: Express) {
app.use(cors());

const storage = multer.diskStorage({
destination: function (_, __, cb) {
cb(null, path.join(process.cwd(), "src/uploads"));
},
filename: function (_, file, cb) {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, file.fieldname + "-" + uniqueSuffix + "-" + file.originalname);
},
});

const upload = multer({ storage: storage });

app.put("/managers/:id/profile-picture", upload.single("file"), (req: Request, res: Response) => {
fs.readFile(`${req.file?.path}`, (err) => {
if (err) {
res.status(500).json({ error: err });
} else {
const managerId = req.params.id
const managerService = new ManagerService();
managerService.updateManager(managerId, { profileImage: req.file?.filename})
res.status(201).json({ status: "success", filename: `/files/${req.file?.filename}` });
}
});
});

app.get("/files/:filename", (req, res) => {
const file = path.join(process.cwd(), "src/uploads", req.params.filename);
fs.readFile(file, (err, data) => {
if (err) {
res.writeHead(404, { "Content-Type": "text" });
res.write("Le fichier n'a pas été trouvé");
res.end();
} else {
res.writeHead(200, { "Content-Type": "application/octet-stream" });
res.write(data);
res.end();
}
});
});
}
2 changes: 1 addition & 1 deletion src/services/manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class ManagerService {

async updateManager(
id: string,
data: Partial<Pick<ManagerEntity, "firstName" | "lastName" | "role">>
data: Partial<Pick<ManagerEntity, "firstName" | "lastName" | "role" | "profileImage">>
) {
const managerFound = await this.getManagerById(id);
const updatedManager = this.db.merge(managerFound, data);
Expand Down
1 change: 1 addition & 0 deletions src/typeDefs/manager.gql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Manager {
lastName: String!
email: String!
role: ManagerRole!
pictureImage: String
isGloballyActive: Boolean!
authorizations: [Authorization!]!
company: Company!
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.