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

Development3 #235

Open
wants to merge 5 commits into
base: master3
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 166 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,169 @@ Returns the new Team Members list:
"role": "member"
}
]
```
```

### Get All Users With User Permissions /moderators

- **GET**

Returns all users list:
```javascript
[
{
"id": 42,
"username": "Aaliyah.Swaniawski",
"email": "[email protected]",
"status": "active",
"user_permissions": "basic"
}
]
```

### Get an Individual User From The Users Table With User permissions /moderators/:id

- **GET**

Expects params:id

Returns the user:
```javascript
[
{
"id": 401,
"username": "james",
"email": "[email protected]",
"status": "active",
"user_permissions": "moderator"
}
]
```


### Change User Permissions To Moderator /moderators/changeToMod/:user_id

- **GET**

Expects params:user_id

Returns 1

### Change User Permissions To Basic /moderators/changeToBasic/:user_id

- **GET**

Expects params:user_id

Returns 1

### Insert Deleted Post and Moderator Who Deleted The Post /posts/insert-deleted-post/:user_id

- **POST**

Expects params:user_id
Expects postBody, id:
```javascript
[
{
id: 46,
body: 'Im an old man that used to love the twilight zone, I think a modern revamp is necessary to incorporate modern pro
}
]
```

Returns
```javascript
Result {
command: 'INSERT',
rowCount: 1,
oid: 0,
rows: [],
fields: [],
_parsers: [],
RowCtor: null,
rowAsArray: false,
_getTypeParser: [Function: bound ]
}
```

### Get Deleted Post /posts/get-deleted-post

- **GET**

Returns
```javascript
[
{
"id": 1,
"post": "Im an old man that used to love the twilight zone, I think a modern revamp is necessary to incorporate modern problems.",
"post_id": 46,
"username": "imon"
}
]
```

### Get All Approved Emails /emails

- **GET**

Returns
```javascript
[
{
"id": 1,
"email": "[email protected]",
"created_at": "2019-04-19T20:18:57.138Z",
"first_name": "john",
"last_name": "doe"
}
]
```


### Check If Email Is In The Approved Email Database

- **GET**

Expects token:
email: eyJhbGciOiJIUzI1NiIsIdR5cCI6IkpXVCJ9.eyJpZCI6NDA3LCJ1c2VybmFtZSI6Imltb24iLCJlbWFpbCI6Imltb25vdmJ1ZGVAZa1haWwuY29
tIiwidG90YWxfaG91cnMijjQzMjI4MS4zNTY1OTUyNzc4LCJpYXQiOjE1NTYxMjY0ODMsImV4cCI6MTU1NjI5OTI4M30.6pNLf47NaaKGw57ErM5IZzTe-A
5Ld-oR_DC0MLv_AkQ

Returns
```javascript
[
{ id: 3,
email: '[email protected]',
created_at: 2019-04-19T20:18:57.138Z,
first_name: 'your name',
last_name: 'your name' }
]
```


### Post A New Email /emails

- **POST**

Expects body
```javascript
{
email: '[email protected]'
}
```

Returns
```javascript
{
"message": "Successfully added!"
}
```


### Delete An Email emails/:id

- **DELETE**

Expects params: id

Returns 1
116 changes: 59 additions & 57 deletions backend/db/models/emailDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,81 @@ const db = require('../dbConfig');
const fs = require('fs');
const csv = require('fast-csv');


// Get all emails from the table
const getEmails = () => {
return db('approved_emails')

return db('approved_emails');
};

// Add email to the table
const insertEmail = email => {
console.log('insert email:', email)
return db('approved_emails')
.insert(email)
}
const insertEmail = (email) => {
console.log('insert email:', email);
return db('approved_emails').insert(email);
};

const insertEmailv2 = async email => {
const newemail = await db('approved_emails').insert(email);
const insertEmailv2 = async (email) => {
const newemail = await db('approved_emails').insert(email);

return newemail;
}
return newemail;
};

// Add csv file to approved_emails database
const csvInsert = csvFile => {
let counter = 0;
const csvInsert = (csvFile) => {
let counter = 0;

let csvStream = csv.fromString(csvFile, { headers: true })
.on('data', (record) => {
csvStream.pause();
console.log(record)
if (counter < 10) {
const email = record.email;
const first_name = record.first_name;
const last_name = record.last_name;
let csvStream = csv
.fromString(csvFile, { headers: true })
.on('data', (record) => {
csvStream.pause();
console.log(record);
if (counter < 10) {
const email = record.email;
const first_name = record.first_name;
const last_name = record.last_name;

console.log(email)
console.log(email);

db.queryBuilder('INSERT INTO approved_emails(email, first_name, last_name) \
VALUES($1, $2, $3)', [email, first_name, last_name], (err) => {
if (err) {
console.log(err);
};
});
++counter;
}
db.queryBuilder(
'INSERT INTO approved_emails(email, first_name, last_name) \
VALUES($1, $2, $3)',
[ email, first_name, last_name ],
(err) => {
if (err) {
console.log(err);
}
}
);
++counter;
}

csvStream.resume();
}).on('end', () => {
console.log('worked correctly');
}).on('error', (err) => {
console.log(err)
})
csvStream.resume();
})
.on('end', () => {
console.log('worked correctly');
})
.on('error', (err) => {
console.log(err);
});

// const csvFile = csv.map(csv => {
// return csv.split(',')
// })
// // console.log(csvFile)
// const mappedCsvFile = csvFile.map(csvF => {
// console.log(csvF)
// })
// return db('approved_emails')
// .insert({ 'email': data[0], 'first_name': data[1], 'last_name': data[2] })
}
// const csvFile = csv.map(csv => {
// return csv.split(',')
// })
// // console.log(csvFile)
// const mappedCsvFile = csvFile.map(csvF => {
// console.log(csvF)
// })
// return db('approved_emails')
// .insert({ 'email': data[0], 'first_name': data[1], 'last_name': data[2] })
};

// removeEmail from the table
const removeEmail = (id) => {
return db('approved_emails')
.where({ id })
.del()
}
return db('approved_emails').where({ id }).del();
};

module.exports = {
getEmails,
insertEmail,
removeEmail,
csvInsert,
insertEmailv2
}
getEmails,
insertEmail,
removeEmail,
csvInsert,
insertEmailv2
};
Loading