Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
39 changes: 20 additions & 19 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { Config } from "./interface";
import { config as dotenv } from "dotenv";
import { Config } from './interface';
import { config as dotenv } from 'dotenv';

dotenv();

export const configurations: Config = {
application: {
port: process.env.BACKEND_APP_PORT || "",
host: process.env.BACKEND_APP_HOST || "",
name: process.env.NODE_ENV || "",
},
mongodb: {
url: process.env.BACKEND_MONGO_URL || "",
},
awsService: {
name: process.env.DO_BUCKET_NAME || "",
region: process.env.DO_BUCKET_REGION || "",
accessKey: process.env.DO_ACCESS_KEY || "",
secretKey: process.env.DO_SECRET_KEY || "",
endpoint: process.env.DO_ENDPOINT || "",
url: process.env.DO_URL || "",
},
}
application: {
port: process.env.BACKEND_APP_PORT || '',
host: process.env.BACKEND_APP_HOST || '',
name: process.env.NODE_ENV || '',
},
mongodb: {
url:
process.env.BACKEND_MONGO_URL || 'mongodb://localhost:27017/testproject',
},
awsService: {
name: process.env.DO_BUCKET_NAME || '',
region: process.env.DO_BUCKET_REGION || '',
accessKey: process.env.DO_ACCESS_KEY || '',
secretKey: process.env.DO_SECRET_KEY || '',
endpoint: process.env.DO_ENDPOINT || '',
url: process.env.DO_URL || '',
},
};
26 changes: 26 additions & 0 deletions src/database/models/Company.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Schema, model } from 'mongoose';
import { ICompany } from '../../domain/entities/Company';

const CompanySchema = new Schema<ICompany>(
{
name: {
type: String,
},
isDeleted: {
type: Boolean,
default: false,
},
},
{
versionKey: false,
timestamps: true,
toObject: {
virtuals: true,
},
toJSON: {
virtuals: true,
},
},
);

export default model<ICompany>('Company', CompanySchema);
49 changes: 49 additions & 0 deletions src/database/models/Contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Schema, model } from 'mongoose';
import { IContact } from '../../domain/entities/Contacts';

const ConstactSchema = new Schema<IContact>(
{
company: {
type: Schema.Types.ObjectId,
ref: 'Company',
},
firstName: {
type: String,
},
lastName: {
type: String
},
phoneNumber: {
type: String,
},
email: {
type: String,
},
socialMedia: {
type: String,
},
gender: {
type: String,
enum: ['Male', 'Female'],
},
birthDate: {
type: String,
},
isDeleted: {
type: Boolean,
default: false,
},
},
{
versionKey: false,
timestamps: true,
toObject: {
virtuals: true,
},
toJSON: {
virtuals: true,
},
},
);

export default model<IContact>('Contact', ConstactSchema);
52 changes: 26 additions & 26 deletions src/database/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { Schema, model } from "mongoose";
import { IUser } from "../../domain/entities/User";
import { string } from "joi";
import { Schema, model } from 'mongoose';
import { IUser } from '../../domain/entities/User';
import { string } from 'joi';

const UserSchema = new Schema<IUser>(
{
phoneNumber: {
type: String,
},
password: {
type: String
},
isDeleted: {
type: Boolean,
default: false,
},
},
{
versionKey: false,
timestamps: true,
toObject: {
virtuals: true,
},
toJSON: {
virtuals: true,
},
}
{
phoneNumber: {
type: String,
},
password: {
type: String,
},
isDeleted: {
type: Boolean,
default: false,
},
},
{
versionKey: false,
timestamps: true,
toObject: {
virtuals: true,
},
toJSON: {
virtuals: true,
},
},
);

export default model<IUser>("User", UserSchema);
export default model<IUser>('User', UserSchema);
43 changes: 22 additions & 21 deletions src/domain/entities/Contacts.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { ICompany } from "./Company";
import { IFile } from "./File";
import { ICompany } from './Company';
import { IFile } from './File';

export enum Gender {
male = "Male",
female = "Female",
male = 'Male',
female = 'Female',
}

type SocialMedia = {
facebook: string;
instagram: string;
linkedin: string;
telegram: string;
export type SocialMedia = {
facebook: string;
instagram: string;
linkedin: string;
telegram: string;
};

export interface IContact {
id: string;
company: ICompany;
avatar: IFile;
fullName: string;
phoneNumber: string;
email: string;
socialMedia: SocialMedia;
gender: Gender;
birthDate: string;
isDeleted: boolean;
createdAt: Date;
updatedAt: Date;
id: string;
company: ICompany;
avatar: IFile;
firstName: string;
lastName: string;
phoneNumber: string;
email: string;
socialMedia: SocialMedia;
gender: Gender;
birthDate: string;
isDeleted: boolean;
createdAt: Date;
updatedAt: Date;
}
107 changes: 107 additions & 0 deletions src/modules/companies/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Request, Response } from 'express';
import CompaniesService from '../services/index';

class CompanyController {
async createCompany(req: Request, res: Response) {
try {
const {
code,
message,
data: result,
} = await CompaniesService.createCompany(req.body);

res.status(code).send({
code,
msg: message,
data: result,
});
} catch (error) {
res.status(500).send({ msg: 'SERVER_ERROR', data: null });
throw new Error(`CompanyController controller [create] error: ${error}`);
}
}

async getCompany(req: Request, res: Response) {
try {
const {
code,
message,
data: result,
} = await CompaniesService.getCompany();

res.status(code).send({
code,
msg: message,
data: result,
});
} catch (error) {
res.status(500).send({ msg: 'SERVER_ERROR', data: null });
throw new Error(`CompanyController controller [get] error: ${error}`);
}
}

async getCompanyById(req: Request, res: Response) {
try {
const query: any = req.query;

const {
code,
message,
data: result,
} = await CompaniesService.getCompanyById(query);

res.status(code).send({
code,
msg: message,
data: result,
});
} catch (error) {
res.status(500).send({ msg: 'SERVER_ERROR', data: null });
throw new Error(`CompanyController controller [get] error: ${error}`);
}
}

async updateCompany(req: Request, res: Response) {
try {
const query: any = req.query;

const {
code,
message,
data: result,
} = await CompaniesService.updateCompany(query);

res.status(code).send({
code,
msg: message,
data: result,
});
} catch (error) {
res.status(500).send({ msg: 'SERVER_ERROR', data: null });
throw new Error(`Task controller [update] error: ${error}`);
}
}

async removeCompany(req: Request, res: Response) {
try {
const query: any = req.query;

const {
code,
message,
data: result,
} = await CompaniesService.removeCompany(query);

res.status(code).send({
code,
msg: message,
data: result,
});
} catch (error) {
res.status(500).send({ msg: 'SERVER_ERROR', data: null });
throw new Error(`Task controller [delete] error: ${error}`);
}
}
}

export default new CompanyController();
Loading