Skip to content

Commit

Permalink
Merge pull request #38 from fis-g4/task/037
Browse files Browse the repository at this point in the history
Task/037
  • Loading branch information
Alex-GF authored Jan 22, 2024
2 parents 858b92c + c5382be commit ad9767a
Show file tree
Hide file tree
Showing 18 changed files with 591 additions and 352 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ jobs:

- name: Decrypt Google credentials.json
env:
GOOGLE_API_PW: ${{ secrets.GCP_CREDENTIALS_PK }}
GCP_CREDENTIALS: ${{ secrets.ENCODED_GCP_CREDENTIALS }}
run: |
gpg --quiet --batch --yes --decrypt --passphrase="$GOOGLE_API_PW" \
--output $GITHUB_WORKSPACE/GoogleCloudKey.json GoogleCloudKey.json.gpg
echo "$GCP_CREDENTIALS" | base64 --decode > GoogleCloudKey.json
- name: Make envfile
uses: SpicyPizza/[email protected]
Expand Down Expand Up @@ -50,12 +49,16 @@ jobs:
envkey_GCF_VERIFY_TOKEN_ENDPOINT: ${{ vars.GCF_VERIFY_TOKEN_ENDPOINT }}
envkey_GCF_GET_PAYLOAD_FROM_TOKEN_ENDPOINT: ${{ vars.GCF_GET_PAYLOAD_FROM_TOKEN_ENDPOINT }}
envkey_SENDINBLUE_API_KEY: ${{ secrets.CI_SENDINBLUE_API_KEY }}
envkey_COURSES_SERVICE_USERNAME: ${{ secrets.CI_COURSES_SERVICE_USERNAME }}
envkey_COURSES_SERVICE_PASSWORD: ${{ secrets.CI_COURSES_SERVICE_PASSWORD }}
envkey_PAYMENT_SERVICE_USERNAME: ${{ secrets.CI_PAYMENT_SERVICE_USERNAME }}
envkey_PAYMENT_SERVICE_PASSWORD: ${{ secrets.CI_PAYMENT_SERVICE_PASSWORD }}
directory: ./server
file_name: .env
fail_on_empty: false
sort_keys: false

- name: Start MongoDB
- name: Start MongoDBs
uses: supercharge/[email protected]
with:
mongodb-version: ${{ vars.CI_MONGODB_VERSION }}
Expand Down
6 changes: 3 additions & 3 deletions server/db/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import mongoose, { ObjectId } from 'mongoose'
const { Schema } = mongoose

enum PlanType{
FREE = 'FREE',
PREMIUM = 'PREMIUM',
BASIC = 'BASIC',
ADVANCED = 'ADVANCED',
PRO = 'PRO'
}

Expand Down Expand Up @@ -84,7 +84,7 @@ const userSchema = new Schema(
plan: {
type: String,
enum: Object.values(PlanType),
default: PlanType.FREE,
default: PlanType.BASIC,
trim: true,
},

Expand Down
106 changes: 75 additions & 31 deletions server/db/populateInitial.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,124 @@
import { PlanType, User, UserRole } from './models/user';
import bcrypt from 'bcrypt';
import { PlanType, User, UserRole } from './models/user'
import bcrypt from 'bcrypt'

const SALT_ROUNDS: number = parseInt(process.env.SALT_ROUNDS ?? "10");
const POPULATE_DB_ON_EACH_RELOAD: boolean = process.env.RESET_DB_ON_EACH_RELOAD === "true";
const salt = bcrypt.genSaltSync(SALT_ROUNDS);
const SALT_ROUNDS: number = parseInt(process.env.SALT_ROUNDS ?? '10')
const POPULATE_DB_ON_EACH_RELOAD: boolean =
process.env.RESET_DB_ON_EACH_RELOAD === 'true'
const salt = bcrypt.genSaltSync(SALT_ROUNDS)

const bucketUrl = process.env.GCS_BUCKET_URL ?? '';
const bucketName = process.env.GCS_BUCKET_NAME ?? '';
const bucketUrl = process.env.GCS_BUCKET_URL ?? ''
const bucketName = process.env.GCS_BUCKET_NAME ?? ''

function populateUsers() {
User.build({
firstName: 'Maria',
firstName: 'Maria',
lastName: 'Doe',
username: 'mariaDoe',
password: bcrypt.hashSync('maria1234', salt),
email: '[email protected]',
profilePicture: bucketUrl + "/" + bucketName + '/default-user.jpg',
profilePicture: bucketUrl + '/' + bucketName + '/default-user.jpg',
coinsAmount: 0,
plan: PlanType.FREE,
plan: PlanType.BASIC,
role: UserRole.USER,
}).save();
}).save()

User.build({
firstName: 'John',
lastName: 'Doe',
username: 'johnDoe',
password: bcrypt.hashSync('john1234', salt),
email: '[email protected]',
profilePicture: bucketUrl + "/" + bucketName + '/default-user.jpg',
profilePicture: bucketUrl + '/' + bucketName + '/default-user.jpg',
coinsAmount: 100,
plan: PlanType.PREMIUM,
plan: PlanType.ADVANCED,
role: UserRole.USER,
}).save();
}).save()
}

function populateAdmins() {
User.build({
firstName: 'Admin',
lastName: 'User',
username: process.env.ADMIN_USERNAME ?? 'admin',
password: bcrypt.hashSync(process.env.ADMIN_PASSWORD ?? 'password', salt),
password: bcrypt.hashSync(
process.env.ADMIN_PASSWORD ?? 'password',
salt
),
email: process.env.ADMIN_EMAIL ?? '[email protected]',
profilePicture: bucketUrl + "/" + bucketName + '/default-user.jpg',
profilePicture: bucketUrl + '/' + bucketName + '/default-user.jpg',
coinsAmount: 99999,
plan: PlanType.PRO,
role: UserRole.ADMIN,
}).save();
}).save()

User.build({
firstName: 'Courses',
lastName: 'Admin User',
username: process.env.COURSES_SERVICE_USERNAME ?? '',
password: bcrypt.hashSync(process.env.COURSES_SERVICE_PASSWORD ?? '', salt),
password: bcrypt.hashSync(
process.env.COURSES_SERVICE_PASSWORD ?? '',
salt
),
email: '[email protected]',
profilePicture: bucketUrl + "/" + bucketName + '/default-user.jpg',
profilePicture: bucketUrl + '/' + bucketName + '/default-user.jpg',
coinsAmount: 99999,
plan: PlanType.PRO,
role: UserRole.ADMIN,
}).save()

User.build({
firstName: 'Payments',
lastName: 'Admin User',
username: process.env.PAYMENT_SERVICE_USERNAME ?? '',
password: bcrypt.hashSync(
process.env.PAYMENT_SERVICE_PASSWORD ?? '',
salt
),
email: '[email protected]',
profilePicture: bucketUrl + '/' + bucketName + '/default-user.jpg',
coinsAmount: 99999,
plan: PlanType.PRO,
role: UserRole.ADMIN,
}).save();
}).save()
}

async function populateDB() {

if (process.env.NODE_ENV !== 'production') {

console.log('Populating DB...');

User.collection.countDocuments().then((count) => {
if (count === 0 || POPULATE_DB_ON_EACH_RELOAD) {
User.collection.drop().then(()=>{
console.log('Populating DB with example users...')

User.collection.drop().then(() => {
populateUsers()
});
console.log('Populated!')
})
}

User.find({ role: UserRole.ADMIN })
.countDocuments()
.then((count) => {
if (count === 0 || POPULATE_DB_ON_EACH_RELOAD) {
console.log('Populating DB with admin users...')
populateAdmins()
console.log('Populated!')
}
})
})
} else {
User.find({ role: UserRole.ADMIN }).countDocuments().then((count) => {
if(count === 0) {
console.log('Populating DB with admin users...')
populateAdmins()
console.log('Populated!')
}else{
User.deleteMany({ role: UserRole.ADMIN }).then(() => {
console.log('Populating DB with admin users...')
populateAdmins()
console.log('Populated!')
})
}
})

console.log('Populated!');
}

}

export default populateDB;
export default populateDB
Loading

0 comments on commit ad9767a

Please sign in to comment.