Skip to content

Commit

Permalink
Merge pull request hngprojects#333 from Ibrahim4Grace/feat/contact-us
Browse files Browse the repository at this point in the history
feat: contact us
  • Loading branch information
AdeGneus authored Jul 27, 2024
2 parents 94abdaf + 78f7294 commit debb95b
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 17 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"@types/jest": "^29.5.12",
"@types/multer": "^1",
"@types/node": "^16.18.103",
"@types/stripe": "^8.0.417",
"@types/passport": "^1.0.16",
"@types/stripe": "^8.0.417",
"@types/supertest": "^6.0.2",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
Expand All @@ -63,7 +63,7 @@
"bcryptjs": "^2.4.3",
"bull": "^4.15.1",
"class-validator": "^0.14.1",
"cloudinary": "^2.3.0",
"cloudinary": "^2.3.1",
"config": "^3.3.12",
"cors": "^2.8.5",
"crypto-js": "^4.2.0",
Expand All @@ -89,6 +89,7 @@
"pino": "^9.3.1",
"pino-pretty": "^11.2.1",
"reflect-metadata": "^0.1.14",
"stripe": "^16.5.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"ts-node-dev": "^2.0.0",
Expand Down
123 changes: 123 additions & 0 deletions src/controllers/contactController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Request, Response } from "express";
import { ContactService } from "../services/contactService";
import { validateContact } from "../utils/contactValidator";

const contactService = new ContactService();

/**
* @swagger
* /api/contact:
* post:
* summary: Submit a contact form
* description: Allows users to submit their contact details and message.
* tags:
* - Contact
* requestBody:
* description: Contact details and message
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* example: John Doe
* email:
* type: string
* format: email
* example: [email protected]
* phoneNumber:
* type: string
* example: 1234567890
* message:
* type: string
* example: I would like to inquire about your services.
* required:
* - name
* - email
* - phoneNumber
* - message
* responses:
* 201:
* description: Contact submitted successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Contact submitted successfully
* contact:
* type: object
* properties:
* id:
* type: integer
* example: 1
* name:
* type: string
* example: John Doe
* email:
* type: string
* example: [email protected]
* phoneNumber:
* type: string
* example: 1234567890
* message:
* type: string
* example: I would like to inquire about your services.
* 400:
* description: Bad request, validation failed
* content:
* application/json:
* schema:
* type: object
* properties:
* errors:
* type: array
* items:
* type: string
* example: [ "Please enter a valid email address." ]
* 500:
* description: Internal server error
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* example: Internal server error
*/

export class ContactController {
async createContact(req: Request, res: Response): Promise<void> {
const { name, email, phoneNumber, message } = req.body;

const validationErrors = validateContact({
name,
email,
phoneNumber,
message,
});
if (validationErrors.length > 0) {
res.status(400).json({ errors: validationErrors });
return;
}

try {
const contact = await contactService.createContact({
name,
email,
phoneNumber,
message,
});
res
.status(201)
.json({ message: "Message submitted successfully", contact });
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
}
}
1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from "./PaymentLemonSqueezyController";
export * from "./PaymentController";
export * from "./BlogController";
export * from "./PaymentLemonSqueezyController";
export * from "./contactController";
12 changes: 6 additions & 6 deletions src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ const AppDataSource = new DataSource({
entities: ["src/models/**/*.ts"],
migrations: ["src/migrations/**/*.ts"],
migrationsTableName: "migrations",
ssl: true,
extra: {
ssl: {
rejectUnauthorized: false,
},
},
// ssl: true,
// extra: {
// ssl: {
// rejectUnauthorized: false,
// },
// },
});

export async function initializeDataSource() {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
exportRouter,
sendEmailRoute,
paymentRouter,
contactRouter,
} from "./routes";
import { smsRouter } from "./routes/sms";
import { routeNotFound, errorHandler } from "./middleware";
Expand Down Expand Up @@ -78,6 +79,7 @@ server.use("/api/v1", blogRouter);
server.use("/api/v1/product", productRouter);
server.use("/api/v1/payments", paymentRouter);
server.use("/api/v1/payments/stripe", paymentStripeRouter);
server.use("/api/v1", contactRouter);
server.use("/api/v1/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
server.use("/api/v1/settings", notificationRouter);
server.use("/api/v1/jobs", jobRouter);
Expand Down
19 changes: 19 additions & 0 deletions src/models/contact-us.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class Contact {
@PrimaryGeneratedColumn()
id!: number;

@Column({ type: "varchar", length: 100 })
name!: string;

@Column({ type: "varchar", length: 100 })
email!: string;

@Column({ type: "varchar", length: 20 })
phoneNumber!: number;

@Column({ type: "text" })
message!: string;
}
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from "./category";
export * from "./payment";
export * from "./log";
export * from "./invitation";
export * from "./contact-us";
12 changes: 12 additions & 0 deletions src/routes/contactRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from "express";
import { ContactController } from "../controllers/contactController";

const contactRouter = Router();
const contactController = new ContactController();

contactRouter.post(
"/contact-us",
contactController.createContact.bind(contactController),
);

export { contactRouter };
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export * from "./admin";
export * from "./paymentStripe";
export * from "./export";
export * from "./paymentLemonSqueezy";
// export * from "./payment";
export * from "./contactRoutes";
11 changes: 11 additions & 0 deletions src/services/contactService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AppDataSource from "../data-source";
import { Contact } from "../models/contact-us";

export class ContactService {
private contactRepository = AppDataSource.getRepository(Contact);

async createContact(contactData: Partial<Contact>): Promise<Contact> {
const contact = this.contactRepository.create(contactData);
return this.contactRepository.save(contact);
}
}
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./export.services";
export * from "./sendEmail.services";
export * from "./organization.services";
export * from "./payment/flutter.service";
export * from "./contactService";
Loading

0 comments on commit debb95b

Please sign in to comment.