Skip to content

Commit ab59e53

Browse files
committed
Initialize CouserCounter collection if it's empty
1 parent fced599 commit ab59e53

6 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/apps/backoffice/frontend/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const convictConfig = convict({
1212
doc: 'The Mongo connection URL',
1313
format: String,
1414
env: 'MONGO_URL',
15-
default: 'mongodb://localhost:27017/dev'
15+
default: 'mongodb://localhost:27017/backoffice-frontend-dev'
1616
}
1717
}
1818
});
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
{}
1+
{
2+
"mongo": {
3+
"url": "mongodb://localhost:27017/backoffice-frontend-test"
4+
}
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { CoursesCounterRepository } from '../../../Contexts/Mooc/CoursesCounter/domain/CoursesCounterRepository';
2+
import { CoursesCounter } from '../../../Contexts/Mooc/CoursesCounter/domain/CoursesCounter';
3+
import { CoursesCounterId } from '../../../Contexts/Mooc/CoursesCounter/domain/CoursesCounterId';
4+
import container from './config/dependency-injection';
5+
6+
export async function seed() {
7+
const repository: CoursesCounterRepository = container.get('Mooc.coursesCounter.CoursesCounterRepository');
8+
const logger = container.get('Contexts.shared.Logger');
9+
10+
const alreadyExists = await repository.search();
11+
const isTestEnvironment = process.env.NODE_ENV === 'test';
12+
13+
if (!alreadyExists && !isTestEnvironment) {
14+
logger.info('[Seed] Initializing CourseCounter');
15+
const courseCounter = CoursesCounter.initialize(CoursesCounterId.random());
16+
await repository.save(courseCounter);
17+
}
18+
}

src/apps/backoffice/frontend/server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import errorHandler from 'errorhandler';
22
import app from './app';
33
import container from './config/dependency-injection';
4+
import { seed } from './seed';
45

56
/**
67
* Error Handler. Provides full stack - remove for production
@@ -10,9 +11,11 @@ app.use(errorHandler());
1011
/**
1112
* Start Express server.
1213
*/
13-
const server = app.listen(app.get('port'), () => {
14+
const server = app.listen(app.get('port'), async () => {
1415
const winstonLogger = container.get('Contexts.shared.Logger');
1516

17+
await seed();
18+
1619
winstonLogger.info(
1720
` Backoffice frontend is running at http://localhost:${app.get('port')} in ${app.get('env')} mode`
1821
);

src/apps/mooc_backend/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const convictConfig = convict({
1212
doc: 'The Mongo connection URL',
1313
format: String,
1414
env: 'MONGO_URL',
15-
default: 'mongodb://localhost:27017/dev'
15+
default: 'mongodb://localhost:27017/mooc-backend-dev'
1616
}
1717
}
1818
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"mongo": { "url": "mongodb://localhost:27017/test" }
2+
"mongo": { "url": "mongodb://localhost:27017/mooc-backend-test" }
33
}

0 commit comments

Comments
 (0)