瀏覽代碼

:bug: fixed merge conflicts

tags/0.0.1^2
juancarbajal98 3 年之前
父節點
當前提交
d08a2fed02

+ 5
- 0
backend/lib/index.js 查看文件

@@ -3,6 +3,7 @@ const MembershipPlugin = require('./plugins/membership')
3 3
 const SurveyPlugin = require('./plugins/survey')
4 4
 const ProfilePlugin = require('./plugins/profile')
5 5
 const NotificationPlugin = require('./plugins/notification')
6
+const HealthPlugin = require('./plugins/health')
6 7
 
7 8
 /**
8 9
  * A Hapi server instance
@@ -45,5 +46,9 @@ exports.plugin = {
45 46
         await server.register(NotificationPlugin, {
46 47
             routes: { prefix: '/notification' },
47 48
         })
49
+
50
+        await server.register(HealthPlugin, {
51
+            routes: { prefix: '/health' },
52
+        })
48 53
     },
49 54
 }

+ 9
- 0
backend/lib/plugins/health.js 查看文件

@@ -0,0 +1,9 @@
1
+const HealthRoute = require('../routes/health/get')
2
+
3
+module.exports = {
4
+    name: 'health-plugin',
5
+    version: '1.0.0',
6
+    register: async (server, options) => {
7
+        await server.route(HealthRoute)
8
+    },
9
+}

+ 2
- 0
backend/lib/plugins/user.js 查看文件

@@ -14,6 +14,7 @@ const UserSignupRoute = require('../routes/user/signup')
14 14
 
15 15
 const UserService = require('../services/user')
16 16
 const DisplayService = require('../services/display')
17
+const HealthService = require('../services/health')
17 18
 
18 19
 module.exports = {
19 20
     name: 'user-plugin',
@@ -38,6 +39,7 @@ module.exports = {
38 39
         await server.register(Schmervice)
39 40
         server.registerService(UserService)
40 41
         server.registerService(DisplayService)
42
+        server.registerService(HealthService)
41 43
 
42 44
         await server.route(UserCurrentRoute)
43 45
         await server.route(UserLoginRoute)

+ 69
- 0
backend/lib/routes/health/get.js 查看文件

@@ -0,0 +1,69 @@
1
+'use strict'
2
+
3
+const apiSchema = require('../../schemas/api')
4
+const errorSchema = require('../../schemas/errors')
5
+const healthSchema = require('../../schemas/health')
6
+
7
+const pluginConfig = {
8
+    handlerType: 'health',
9
+    docs: {
10
+        description: 'Get server stats',
11
+        notes: 'Returns stats on server status'
12
+    }
13
+}
14
+
15
+const validators = {}
16
+
17
+const responseSchemas = {
18
+    health: healthSchema.stats,
19
+    error: errorSchema.single
20
+}
21
+
22
+module.exports = {
23
+    method: 'GET',
24
+    path: '/',
25
+    options:{
26
+        ...pluginConfig.docs,
27
+        tags: ['api'],
28
+        auth: false,
29
+        cors: true,
30
+        handler: async function (request, h) {
31
+            const { healthService } = request.server.services()
32
+            const stats = await healthService.getStats()
33
+            try {
34
+                return h.response(({
35
+                    ok:true,
36
+                    handler: pluginConfig.handlerType,
37
+                    data: stats
38
+                })).code(200)
39
+            } catch (err) {
40
+                return h
41
+                    .response({
42
+                        ok: false,
43
+                        handler: pluginConfig.handlerType,
44
+                        data: {error: `${err}`}
45
+                    })
46
+                    .code(409)
47
+            }
48
+        },
49
+        validate: {
50
+            ...validators,
51
+            failAction: 'log'
52
+        },
53
+
54
+        response: {
55
+            status: {
56
+                200: apiSchema.single
57
+                    .append({
58
+                        data: responseSchemas.health,
59
+                    })
60
+                    .label('api_single_res'),
61
+                409: apiSchema.single
62
+                    .append({
63
+                        data: responseSchemas.error,
64
+                    })
65
+                    .label('error_single_res'),
66
+            },
67
+        },
68
+    },
69
+}

+ 1
- 0
backend/lib/routes/notification/index.js 查看文件

@@ -36,6 +36,7 @@ module.exports = {
36 36
                 name: 'BDGRS',
37 37
                 price: (500 + Math.floor(Math.random() * 100)).toString(),
38 38
                 order: null,
39
+                type: 'info',
39 40
             }
40 41
 
41 42
             // Write to the input stream

+ 10
- 0
backend/lib/schemas/health.js 查看文件

@@ -0,0 +1,10 @@
1
+'use strict'
2
+
3
+const Joi = require('Joi')
4
+
5
+const stats = Joi.object({
6
+    date: Joi.string().required(),
7
+    users: Joi.number().required(),
8
+}).label('stats')
9
+
10
+module.exports = { stats }

+ 18
- 0
backend/lib/services/health.js 查看文件

@@ -0,0 +1,18 @@
1
+const Schmervice = require('@hapipal/schmervice')
2
+
3
+module.exports = class HealthService extends Schmervice.Service {
4
+    constructor(...args){
5
+        super(...args)
6
+    }
7
+
8
+    /**
9
+     * Returns date and number of users
10
+     * @returns {object}
11
+     */
12
+    async getStats(){
13
+        const { User } = this.server.models()
14
+        const users = await User.query()
15
+        const date = new Date()
16
+        return { date: date.toString(), users: users.length }
17
+    }
18
+}

+ 0
- 4
backend/server/manifest.js 查看文件

@@ -1,14 +1,10 @@
1 1
 const Dotenv = require('dotenv').config({path: './server/.env'})
2
-// const Dotenv = require('dotenv')
3 2
 const Confidence = require('@hapipal/confidence')
4 3
 const Inert = require('@hapi/inert')
5 4
 const Vision = require('@hapi/vision')
6 5
 const Schwifty = require('@hapipal/schwifty')
7 6
 const HapiSwagger = require('hapi-swagger')
8 7
 
9
-/** Pull .env into process.env */
10
-// Dotenv.config({ path: `${__dirname}/../.env` })
11
-// Dotenv.config({ path: `${__dirname}/../.env` })
12 8
 
13 9
 /** Glue manifest as a confidence store */
14 10
 module.exports = new Confidence.Store({

+ 14
- 21
frontend/src/App.vue 查看文件

@@ -24,7 +24,7 @@ w-app
24 24
 import 'wave-ui/dist/wave-ui.css'
25 25
 import SideBar from './components/SideBar.vue'
26 26
 
27
-import { Chatter, currentProfile, StonkAlert } from './services'
27
+import { currentProfile } from './services'
28 28
 import { surveyFactory } from './utils'
29 29
 
30 30
 const DEV_MODE = import.meta.env.VITE_DEV == 'true'
@@ -52,33 +52,26 @@ export default {
52 52
         if (DEV_MODE) {
53 53
             this.setPid(DEV_PID)
54 54
         }
55
-
56
-        if (currentProfile.isLoggedIn) {
57
-            console.warn(`setting up Chatter and Toaster for ${this.getPid}...`)
58
-            this.setupChatter()
59
-            this.setupToaster()
60
-        }
61
-        console.log('---')
62 55
     },
63 56
     methods: {
64 57
         /**
65 58
          * Sync up this components state with
66 59
          * the currentProfile handler
67 60
          */
68
-        setPid(profileId) {
69
-            currentProfile.login(profileId)
70
-        },
61
+        async setPid(profileId) {
62
+            if (currentProfile.isLoggedIn) {
63
+                currentProfile.logout()
64
+            }
65
+            await currentProfile.login(profileId)
71 66
 
72
-        /**
73
-         * For push notifications and chat
74
-         */
75
-        setupToaster() {
76
-            const t = new StonkAlert(this.getPid)
77
-        },
78
-        setupChatter() {
79
-            const c = new Chatter()
80
-            const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
81
-            c.setup(testAccountUUID)
67
+            if (currentProfile.isLoggedIn) {
68
+                console.warn(
69
+                    `setting up Chatter and Toaster for ${this.getPid}...`,
70
+                )
71
+                currentProfile.setupChatter()
72
+                currentProfile.setupToaster(this.$waveui.notify)
73
+            }
74
+            console.log('---')
82 75
         },
83 76
     },
84 77
 }

+ 39
- 10
frontend/src/services/login.service.js 查看文件

@@ -1,5 +1,5 @@
1 1
 import { ref } from 'vue'
2
-import { fetchResponsesByProfileId } from '../services'
2
+import { fetchResponsesByProfileId, Chatter, StonkAlert } from '../services'
3 3
 import { surveyFactory } from '../utils'
4 4
 
5 5
 /**
@@ -9,12 +9,15 @@ import { surveyFactory } from '../utils'
9 9
 class Login {
10 10
     constructor() {
11 11
         this._loading = false
12
-        
12
+
13 13
         // Make reactive with vue observer
14 14
         this.id = ref(null)
15 15
 
16 16
         this.responses = []
17 17
         this.tags = []
18
+
19
+        this.toaster = null
20
+        this.chatter = null
18 21
     }
19 22
     get isLoading() {
20 23
         return this._loading
@@ -30,12 +33,15 @@ class Login {
30 33
     }
31 34
     /**
32 35
      * Combine questions retrieved from the database and
33
-     * questions defined in out lang file and 
36
+     * questions defined in out lang file and
34 37
      * copare to responses stored
35 38
      * @returns {boolean}
36 39
      */
37 40
     get isComplete() {
38
-        return this.responses.length == surveyFactory.questionsFromDb.length && surveyFactory.questionsFromDb.length > 0
41
+        return (
42
+            this.responses.length == surveyFactory.questionsFromDb.length &&
43
+            surveyFactory.questionsFromDb.length > 0
44
+        )
39 45
     }
40 46
     /**
41 47
      * Check that some responses are set
@@ -43,20 +49,28 @@ class Login {
43 49
      */
44 50
     get hasResponses() {
45 51
         return this.responses.length && this.responses.length > 0
46
-    } 
52
+    }
47 53
 
48 54
     /**
49 55
      * Login a profile by id number
50 56
      * @param {number} profileId
51 57
      * @returns {number} stored reactive id
52 58
      */
53
-     async login(profileId) {
59
+    async login(profileId) {
54 60
         console.warn('logging in:', profileId)
55 61
         this.id.value = parseInt(profileId)
56 62
         return this.id.value
57 63
     }
58
-    logout() { this.id.value = null }
59
-    
64
+    logout() {
65
+        this.id.value = null
66
+        if (this.toaster) {
67
+            this.toaster.stop()
68
+        }
69
+        if (this.chatter) {
70
+            this.toaster.stop()
71
+        }
72
+    }
73
+
60 74
     async getTags() {
61 75
         try {
62 76
             const tags = []
@@ -65,7 +79,9 @@ class Login {
65 79
             console.error(err)
66 80
         }
67 81
     }
68
-    setTags(tags) { this.tags = tags }
82
+    setTags(tags) {
83
+        this.tags = tags
84
+    }
69 85
 
70 86
     async getResponses() {
71 87
         try {
@@ -75,7 +91,20 @@ class Login {
75 91
             console.error(err)
76 92
         }
77 93
     }
78
-    setResponses(responses) { this.responses = responses }
94
+    setResponses(responses) {
95
+        this.responses = responses
96
+    }
97
+    /**
98
+     * For push notifications and chat
99
+     */
100
+    setupToaster(waveCb) {
101
+        this.toaster = new StonkAlert(this.id.value, waveCb)
102
+    }
103
+    setupChatter() {
104
+        this.chatter = new Chatter()
105
+        const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
106
+        this.chatter.setup(testAccountUUID)
107
+    }
79 108
 }
80 109
 
81 110
 const currentProfile = new Login()

+ 11
- 5
frontend/src/services/notification.service.js 查看文件

@@ -14,22 +14,28 @@ class Toaster {
14 14
     listenFor(event, callback) {
15 15
         this.source.addEventListener(event, callback)
16 16
     }
17
+    stop() {
18
+        this.source.close()
19
+    }
17 20
 }
18 21
 
19 22
 /**
20 23
  * Example extension that listens for 'stonk' events
21 24
  */
22 25
 class StonkAlert extends Toaster {
23
-    constructor(profileId) {
26
+    constructor(profileId, waveCb) {
24 27
         super(profileId)
25
-
28
+        this.event = 'stonk'
26 29
         this.stonks = {}
27
-        this.listenFor(`${profileId}.stonk`, message => {
30
+        this.listenFor(`${profileId}.${this.event}`, message => {
28 31
             const parsed = JSON.parse(message.data)
29 32
             this.stonks[parsed.name] = parsed
30
-            console.log('updated:', this.stonks)
33
+            waveCb(this._formatToast(parsed), parsed.type)
31 34
         })
32 35
     }
36
+    _formatToast(parsed) {
37
+        return `${parsed.name}: ${parsed.profile_id} ${parsed.order} at ${parsed.price}`
38
+    }
33 39
 }
34 40
 
35
-export { Toaster, StonkAlert }
41
+export { Toaster, StonkAlert }

Loading…
取消
儲存