Skip to content

Commit

Permalink
Add checkin data
Browse files Browse the repository at this point in the history
  • Loading branch information
sailxjx committed Jan 29, 2016
1 parent 1ea241d commit c723444
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 26 deletions.
41 changes: 15 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
# Logs
logs
lib-cov
*.seed
*.log
npm-debug.log*

# Runtime data
pids
*.csv
*.dat
*.out
*.pid
*.seed
*.gz

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
pids
logs
results

# Dependency directory
npm-debug.log
node_modules

# Optional npm cache directory
.npm
config/*
!config/default.coffee
!config/test.coffee

# Optional REPL history
.node_repl_history
db/*
!db/.gitkeep
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git*
test/
src/
.DS_Store
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "0.10"
- "0.11"
- "0.12"
- "4"
- "5"
16 changes: 16 additions & 0 deletions app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
express = require 'express'
logger = require 'graceful-logger'
config = require 'config'
bodyParser = require 'body-parser'
app = express()

bot = require './src/bot'

app.use bodyParser.json(limit: '10mb')
app.use bodyParser.urlencoded(extended: true, limit: '10mb')

app.bot = bot app

app.listen config.port, -> logger.info "Bot listen on #{config.port}"

module.exports = app
3 changes: 3 additions & 0 deletions config/default.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports =
port: 8201
dbPath: 'db'
1 change: 1 addition & 0 deletions config/test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Empty file added db/.gitkeep
Empty file.
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "talk-checkin-bot",
"version": "0.0.0",
"description": "talk-checkin-bot",
"main": "./lib/index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "NODE_ENV=test ./node_modules/.bin/mocha --require coffee-script/register --require should --reporter spec test/main.coffee"
},
"repository": {
"type": "git",
"url": "[email protected]:jianliaoim/talk-checkin-bot.git"
},
"author": {
"name": "Xu Jingxin",
"email": "[email protected]"
},
"license": "MIT",
"devDependencies": {
"mocha": "^2.4.5",
"should": "^8.2.1",
"supertest": "^1.1.0"
},
"dependencies": {
"bluebird": "^3.1.5",
"body-parser": "^1.14.2",
"coffee-script": "^1.10.0",
"config": "^1.19.0",
"cron": "^1.1.0",
"express": "^4.13.4",
"graceful-logger": "^0.4.3",
"lodash": "^4.0.1",
"moment": "^2.11.1",
"nedb": "^1.7.2"
}
}
3 changes: 3 additions & 0 deletions reply.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
replies = [

]
25 changes: 25 additions & 0 deletions src/bot.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
config = require 'config'
model = require './model'

class Bot

module.exports = (app) ->

bot = new Bot

app.post '/messages', (req, res) ->

return res.status(400).send({error: "Missing creator"}) unless req.body.creator

checkinData =
_creatorId: req.body.creator._id
name: req.body.creator.name
time: Date.now()

model.addCheckin checkinData

.then -> res.status(200).send ok: 1

.catch (err) -> res.status(400).send error: err.message

bot
57 changes: 57 additions & 0 deletions src/model.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
path = require 'path'
Datastore = require 'nedb'
config = require 'config'
moment = require 'moment'
Promise = require 'bluebird'
logger = require 'graceful-logger'
_ = require 'lodash'
fs = require 'fs'

dbs = {}

Promise.promisifyAll fs
Promise.promisifyAll Datastore.prototype

model =

getDb: ->
today = moment().format('YYYYMMDD')
dbName = "checkin_#{today}.json"
unless dbs[dbName]
dbs[dbName] = new Datastore
filename: path.join(config.dbPath, dbName)
autoload: true
dbs[dbName]

destroyDb: ->
today = moment().format('YYYYMMDD')
dbName = "checkin_#{today}.json"
delete dbs[dbName]
fs.unlinkAsync path.join(config.dbPath, dbName)
.catch (err) ->

addCheckin: (checkinData) ->
db = model.getDb()

$firstCheckinData = db.findOneAsync _creatorId: checkinData._creatorId, type: 'first'

.then (firstCheckinData) ->
return firstCheckinData if firstCheckinData
db.insertAsync _.assign {}, checkinData, type: 'first'

$lastCheckinData = db.updateAsync
_creatorId: checkinData._creatorId
type: 'last'
,
_.assign {}, checkinData, type: 'last'
,
upsert: true
returnUpdatedDocs: true

Promise.all [$firstCheckinData, $lastCheckinData]

getCheckins: ->
db = model.getDb()
db.findAsync({})

module.exports = model
Empty file added src/rules.coffee
Empty file.
54 changes: 54 additions & 0 deletions test/main.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
should = require 'should'
fs = require 'fs'
moment = require 'moment'
app = require '../app'
model = require '../src/model'
request = require 'supertest'

cleanup = (done) -> model.destroyDb().nodeify done

describe 'Bot', ->

before cleanup

it 'should record the first checkin time of user', (done) ->

request(app).post '/messages'
.set 'Content-Type': 'application/json'
.send
creator:
_id: 1
name: 'xxx'
body: 'Hello'
.end (err, res) ->
res.body.should.eql ok: 1
done err

it 'should update the last visit time of user', (done) ->

# Checkin again
request(app).post '/messages'
.set 'Content-Type': 'application/json'
.send
creator:
_id: 1
name: 'xxx'
body: 'Hello Again'
.end (err, res) ->
res.body.should.eql ok: 1
done err

it 'should get all the checkin data', (done) ->

model.getCheckins()

.then (datas) ->
datas.length.should.eql 2
datas.forEach (data) -> data._creatorId.should.eql 1
datas.sort (x, y) -> if x.time > y.time then -1 else 1
datas[0].type.should.eql 'last'
datas[1].type.should.eql 'first'

.nodeify done

after cleanup

0 comments on commit c723444

Please sign in to comment.