Skip to content

Commit 4ae99c6

Browse files
author
darkhorse-coder
committed
initial commit
0 parents  commit 4ae99c6

File tree

9,332 files changed

+1217124
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

9,332 files changed

+1217124
-0
lines changed

.env

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NODE_ENV = dev
2+
PORT = 5000
3+
DB_URL = mongodb+srv://challengeUser:[email protected]/getir-case-study?retryWrites=true

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modeules

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Node_backend_codebase
2+
Simple Node/Express/MongoDB backend service.\
3+
DB: MongoDB Atlas.
4+
5+
6+
## Available Scripts
7+
8+
In the project directory, you can run:
9+
10+
### `npm start`
11+
12+
Runs the server in the development mode.\
13+
http://localhost:5000
14+
15+
# Features
16+
- User Authorization using JWT (user/login, user/register).
17+
- Request validate using Joi validation.
18+
- Smart query string parsing for get end points.
19+

__tests__/records.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const mongoose = require("mongoose");
2+
const app = require("../app");
3+
const request = require("supertest");
4+
process.env.NODE_ENV = "test";
5+
6+
describe("POST /records", () => {
7+
test("The 'records' is array of object as 'key', 'createdAt', 'totalCount'", async () => {
8+
const response = await request(app).post("/records").send({
9+
startDate: "2015-03-08",
10+
endDate: "2015-04-30",
11+
minCount: 2700,
12+
maxCount: 3000,
13+
});
14+
expect(response.body.records.length).toBe(19);
15+
expect(response.body.records[0]).toHaveProperty("key");
16+
expect(response.body.records[0]).toHaveProperty("createdAt");
17+
expect(response.body.records[0]).toHaveProperty("totalCount");
18+
expect(response.statusCode).toBe(200);
19+
});
20+
21+
afterAll((done) => {
22+
mongoose.connection.close()
23+
done();
24+
});
25+
});

app.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const express = require("express");
2+
require('dotenv').config();
3+
const morgan = require('morgan');
4+
const cookieParser = require('cookie-parser');
5+
const bodyParser = require('body-parser');
6+
const cors = require("cors");
7+
const mongoose = require('mongoose');
8+
const routes = require('./routes/index');
9+
10+
mongoose.Promise = global.Promise;
11+
try{
12+
mongoose.connect(process.env.DB_URL, {
13+
useNewUrlParser: true,
14+
useUnifiedTopology: true,
15+
useCreateIndex: true,
16+
useFindAndModify: false
17+
});
18+
}catch(e){
19+
console.error(e)
20+
}
21+
22+
const app = express();
23+
app.use(cors());
24+
app.use(morgan('dev'))
25+
app.use(bodyParser.json({limit: '5mb'}));
26+
app.use(bodyParser.urlencoded({ extended: true }));
27+
app.use(cookieParser());
28+
29+
// Routes
30+
app.use('/', routes);
31+
32+
module.exports = app;

controllers/record.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const moment = require("moment-timezone");
2+
const recordModel = require("../models/record");
3+
4+
exports.retrieve = async (req, res, next) => {
5+
try {
6+
const records = await recordModel.aggregate([
7+
{
8+
$match: {
9+
createdAt: {
10+
$gte: moment(req.body.startDate, "YYYY-MM-DD").toDate(),
11+
$lte: moment(req.body.endDate, "YYYY-MM-DD").toDate(),
12+
},
13+
},
14+
},
15+
16+
{
17+
$project: {
18+
_id: 0,
19+
key: "$key",
20+
createdAt: "$createdAt",
21+
totalCount: { $sum: "$counts" },
22+
},
23+
},
24+
25+
{
26+
$match: {
27+
totalCount: { $gte: req.body.minCount, $lte: req.body.maxCount },
28+
},
29+
},
30+
]);
31+
32+
return res.status(200).json({ code: 0, msg: "Success", records });
33+
} catch (error) {
34+
next(error);
35+
}
36+
};

models/record.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const mongoose = require("mongoose");
2+
3+
const recordSchema = new mongoose.Schema(
4+
{
5+
key: {
6+
type: String,
7+
required: true,
8+
},
9+
10+
value: {
11+
type: String,
12+
required: true,
13+
},
14+
15+
counts: [
16+
{
17+
type: Number,
18+
},
19+
],
20+
21+
createdAt: {
22+
type: String,
23+
required: true,
24+
},
25+
26+
totalCount: {
27+
type: Number,
28+
required: true,
29+
}
30+
},
31+
{
32+
versionKey: false,
33+
timestamps: true,
34+
}
35+
);
36+
37+
module.exports = mongoose.model("record", recordSchema);

node_modules/.bin/acorn

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/atob

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/browserslist

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/escodegen

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/esgenerate

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/esparse

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/esvalidate

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/import-local-fixture

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/is-ci

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/is-docker

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/jest

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/jest-runtime

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/js-yaml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/jsesc

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/json5

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mime

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/node-which

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/parser

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/rimraf

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/sane

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/semver

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/sshpk-conv

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/sshpk-sign

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/sshpk-verify

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/uuid

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/watch

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)