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

Init Payments, Reviews and Users Models #48

Merged
merged 17 commits into from
Oct 16, 2022
19 changes: 18 additions & 1 deletion server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import dotenv from 'dotenv';
import cookieParser from 'cookie-parser';
import { join } from 'path';
import morgan from 'morgan';
import { Announcement, Advertisement } from './models';
import {
Announcement, Advertisement, User, Payment, Review,
} from './models';

dotenv.config();

Expand Down Expand Up @@ -35,6 +37,21 @@ app.get('/advertisements', async (req, res) => {
res.json({ message: 'Advertisements', data });
});

app.get('/Users', async (req, res) => {
const data = await User.findAll();
res.json({ message: 'Users', data });
});

app.get('/Payments', async (req, res) => {
const data = await Payment.findAll();
res.json({ message: 'Payments', data });
});

app.get('/Reviews', async (req, res) => {
const data = await Review.findAll();
res.json({ message: 'Reviews', data });
});

if (NODE_ENV === 'production') {
app.use(express.static(join(__dirname, '..', 'client', 'build')));
app.get('*', (req, res) => {
Expand Down
15 changes: 10 additions & 5 deletions server/database/config/build.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import sequelize from './connection';
import {
Announcements, Advertisements,
} from './fakeData';
import
{
Announcements, Advertisements, Users, Reviews, Payments,
}
from './fakeData';

import {
Announcement, Advertisement,
} from '../../models/index';
Announcement, Advertisement, Review, User, Payment,
} from '../../models';

const insertDB = async () => {
try {
await sequelize.sync({ force: true });
await Announcement.bulkCreate(Announcements);
await Advertisement.bulkCreate(Advertisements);
await User.bulkCreate(Users);
await Review.bulkCreate(Reviews);
await Payment.bulkCreate(Payments);

// eslint-disable-next-line no-console
console.log('Build Database Successfully');
Expand Down
127 changes: 112 additions & 15 deletions server/database/config/fakeData.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,112 @@
// const AnnouncementsType = {
// title: string,
// startDate: string,
// endDate: string,
// };
const Users = [
{
id: 1,
first_name: 'أحمد',
last_name: 'سعيد',
email: '[email protected]',
phone_number: '0591000100',
roles: 'ADMIN',
hased_password: '$2y$10$MKpnqAAinu5wjl/MYRc3U.XApWTaEexvI4DMaJA.XZTou5lvpSPfu',
},
{
id: 2,
first_name: 'خالد',
last_name: 'عمر',
email: '[email protected]',
phone_number: '235-783-1177',
roles: 'USER',
hased_password: '$2y$10$ROMcXqVNwbepoV6ZdTsTa.6/iego9a72vzSK70pF/S8Oxe/y8tLeO',
},
{
id: 3,
first_name: 'سمير',
last_name: 'عبد الله',
email: '[email protected]',
phone_number: '498-722-9258',
roles: 'USER',
hased_password: '$2y$10$NrRa5y454oIukcfU/iNDruP2iEJPm9UGlHqyhZiyd5mY1fy2vULkG',
},
{
id: 4,
first_name: 'مهند',
last_name: 'محمد',
email: '[email protected]',
phone_number: '172-408-5906',
roles: 'USER',
hased_password: '$2y$10$dOSPYdqciJCM9U10HiWtCOg9j6jpMD69rMPV/2LI/dDL4SxLATkNG',
},
{
id: 5,
first_name: 'تميم',
last_name: 'سليم',
email: '[email protected]',
phone_number: '910-220-7086',
roles: 'USER',
hased_password: '$2y$10$6Y1AcLQDLM7vSSu1A5dj9e61YgCFACEXoBP3NxJ4qX/1SeUUXLFtm',
},
];

const Reviews = [
{
title: 'ادارة رائعة',
description: 'يوجد في البرج ادارة قادرة على ادارة جميع خدمات البرج بكفائة',
points: 2,
is_shown: true,
user_id: 2,
},
{
title: 'حارس أمين',
description: 'يود في البرج حارس أمين يخدم السكان',
points: 0,
is_shown: true,
user_id: 3,
},
{
title: 'خدمة سيئة',
description: 'خدمات البرج سيئة و يفتقر للنظافة',
points: 2,
is_shown: false,
user_id: 4,
},
];

const Payments = [
{
title: 'دفع فاتورة',
description:
'لقد قمت بدفع فاتورة بقيمة 80 شيكل',
user_id: 4,
},
{
title: 'دفع فاتورة',
description:
'لقد قمت بدفع فاتورة بقيمة 70 شيكل',
user_id: 3,
},
{
title: 'دفع فاتورة',
description:
'لقد قمت بدفع فاتورة بقيمة 90 شيكل',
user_id: 5,
},
];

const Announcements = [
{
title: 'تنبيه لسكان البرج الكرام: فصل الطاقة الشمسية على الساعة السابعة مساء اليوم',
title:
'تنبيه لسكان البرج الكرام: فصل الطاقة الشمسية على الساعة السابعة مساء اليوم',
start_date: '20/10/2022',
end_date: '21/10/2022',
},
{
title: ' تنبيه لسكان البرج الكرام: نعلمكم بأنه سيتم اليوم اغلاق المصعد لأغراض الصيانة على الساعة السادسة مساء لمدة ساعة',
title:
' تنبيه لسكان البرج الكرام: نعلمكم بأنه سيتم اليوم اغلاق المصعد لأغراض الصيانة على الساعة السادسة مساء لمدة ساعة',
start_date: '20/10/2022',
end_date: '21/10/2022',
},
{
title: 'اليوم سيتم الاحتفال بذكرى انشاء البرج الثالثة الساعة السابعة مساء على الروف نتمنى حضوركم',
title:
'اليوم سيتم الاحتفال بذكرى انشاء البرج الثالثة الساعة السابعة مساء على الروف نتمنى حضوركم',
start_date: '20/10/2022',
end_date: '21/10/2022',
},
Expand All @@ -24,26 +115,32 @@ const Announcements = [
const Advertisements = [
{
title: 'شقة متاحة',
description: ' متاح لدينا شقة فارغة بمساحة 140 متر مربع تحتوي على 3 غرف نوم ومطبخ على الطابق الرابع للمزيد من المعلومات يرجى التواصل',
description:
' متاح لدينا شقة فارغة بمساحة 140 متر مربع تحتوي على 3 غرف نوم ومطبخ على الطابق الرابع للمزيد من المعلومات يرجى التواصل',
start_date: '20/10/2022',
end_date: '30/10/2022',
image: 'https://images.pexels.com/photos/7534561/pexels-photo-7534561.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
image:
'https://images.pexels.com/photos/7534561/pexels-photo-7534561.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
},
{
title: 'مطلوب عامل نظافة',
description: 'البرج بحاجة الى عامل نظافة براتب 800 شيكل شهريا ',
start_date: '20/10/2022',
end_date: '30/10/2022',
image: 'https://images.pexels.com/photos/6197123/pexels-photo-6197123.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
image:
'https://images.pexels.com/photos/6197123/pexels-photo-6197123.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
},
{
title: 'شقة متاحة ',
description: 'متاح لدينا شقة فارغة بمساحة 190 متر مربع تحتوي على 3 غرف نوم ومطبخ على الطابق الخامس للمزيد من المعلومات يرجى التواصل',
description:
'متاح لدينا شقة فارغة بمساحة 190 متر مربع تحتوي على 3 غرف نوم ومطبخ على الطابق الخامس للمزيد من المعلومات يرجى التواصل',
start_date: '20/10/2022',
end_date: '30/10/2022',
image: 'https://images.pexels.com/photos/5417293/pexels-photo-5417293.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
image:
'https://images.pexels.com/photos/5417293/pexels-photo-5417293.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
},

];

export { Announcements, Advertisements };
export {
Announcements, Advertisements, Users, Reviews, Payments,
};
29 changes: 29 additions & 0 deletions server/models/Payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
DataTypes,
} from 'sequelize';
import sequelize from '../database/config/connection';

const Payment = sequelize.define(
'Payments',
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
);

export default Payment;
41 changes: 41 additions & 0 deletions server/models/Review.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
DataTypes,
} from 'sequelize';
import sequelize from '../database/config/connection';

const Review = sequelize.define(
'Reviews',
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
points: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
min: 0,
max: 5,
},
},
is_shown: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
);

export default Review;
48 changes: 48 additions & 0 deletions server/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
DataTypes,
} from 'sequelize';
import sequelize from '../database/config/connection';

const User = sequelize.define(
'Transactions',
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
first_name: {
type: DataTypes.STRING,
allowNull: false,
},
last_name: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: true,
validate: {
isEmail: true,
},
},
phone_number: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
},
roles: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
roles: {
role: {

type: DataTypes.ENUM('ADMIN', 'USER'),
allowNull: false,
},
hased_password: {
type: DataTypes.STRING,
validate: {
is: /^[0-9a-f]{64}$/i,
},
allowNull: false,
},
},
);

export default User;
15 changes: 14 additions & 1 deletion server/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import Payment from './Payment';
import Review from './Review';
import User from './User';
import Announcement from './Announcement';
import Advertisement from './Advertisement';

Payment.hasOne(User, {
foreignKey: 'user_id',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
foreignKey: 'user_id',

});
User.belongsTo(Payment);

Review.hasOne(User, {
foreignKey: 'user_id',
});
User.belongsTo(Review);

export {
Announcement, Advertisement,
Payment, Review, User, Announcement, Advertisement,
};