 
 module.exports =
 
-    name: "rescuetime"
+    name: "Rescue Time"
+    slug: "rescuetime"
+    description: "Download all your activities from Rescue Time"
+    vendorLink: "https://www.rescuetime.com/"
+
     fields:
-        apikey: ""
-
-    description: "Fetch all rescuetime data"
-
+        apikey: "text"
     models:
         activities: RescueTimeActivity
+    modelNames: ["RescueTimeActivity" ]
+
 
     # Define model requests.
     init: (callback) ->
-        map = (doc) -> emit(doc.date, doc)
+        map = (doc) -> emit doc.date, doc
         RescueTimeActivity.defineRequest 'byDate', map, (err) ->
             callback err
 
-    # Define parameters (start date and end date) then fetch data accordingly.
+
+    # Get last imported activity to know from where to start the import. Then
+    # define parameters (start date and end date) and fetch data accordingly.
     fetch: (requiredFields, callback) ->
         params = limit: 1, descending: true
         RescueTimeActivity.request 'byDate', params, (err, activities) =>
             if err then callback err
+            else @loadActivities activities, requiredFields, callback
 
-            else
-                apikey = requiredFields.apikey
-                if activities.length > 0
-                    start = moment(activities[0].date).format 'YYYY-MM-DD'
-                else
-                    start = moment().subtract('years', 10).format 'YYYY-MM-DD'
-                end = moment().add('days', 1).format 'YYYY-MM-DD'
 
-                @fetchData apikey, start, end, callback
+    # Depending
+    loadActivities: (activities, requiredFields, callback) ->
+        apikey = requiredFields.apikey
+        end = moment().add('days', 1).format 'YYYY-MM-DD'
+
+        if activities.length > 0
+            start = moment(activities[0].date).format 'YYYY-MM-DD'
+            params = key: moment().format 'YYYY-MM-DD'
+            RescueTimeActivity.requestDestroy 'byDate', params, (err) =>
+                if err then callback err
+                else @fetchData apikey, start, end, callback
+
+        else
+            start = moment().subtract('years', 10).format 'YYYY-MM-DD'
+            @fetchData apikey, start, end, callback
+
+
 
     # Fetch activity list from rescuetime, then create an entry for each row.
     fetchData: (apikey, start, end, callback) ->
         client = request.newClient 'https://www.rescuetime.com/'
         path = 'anapi/data?'
-        path += qs.stringify
+        path += querystring.stringify
             key: apikey
             format: "json"
             perspective: 'interval'

Something went wrong while fetching rescue time data.
"""
            else
                recSave = (i) ->
                    if i < body.rows.length
                        row = body.rows[i]
                        data =
                            date: row[0]
                            duration: row[1]

americano = require 'americano-cozy'
querystring = require 'querystring'
request = require 'request-json'
moment = require 'moment'
log = require('printit')
    date: true
    prefix: 'rescuetime'

# Models

