Просмотр исходного кода

:bug: fixed merge conflicts

tags/0.0.1^2
juancarbajal98 3 лет назад
Родитель
Сommit
d08a2fed02

+ 5
- 0
backend/lib/index.js Просмотреть файл

3
 const SurveyPlugin = require('./plugins/survey')
3
 const SurveyPlugin = require('./plugins/survey')
4
 const ProfilePlugin = require('./plugins/profile')
4
 const ProfilePlugin = require('./plugins/profile')
5
 const NotificationPlugin = require('./plugins/notification')
5
 const NotificationPlugin = require('./plugins/notification')
6
+const HealthPlugin = require('./plugins/health')
6
 
7
 
7
 /**
8
 /**
8
  * A Hapi server instance
9
  * A Hapi server instance
45
         await server.register(NotificationPlugin, {
46
         await server.register(NotificationPlugin, {
46
             routes: { prefix: '/notification' },
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 Просмотреть файл

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

+ 69
- 0
backend/lib/routes/health/get.js Просмотреть файл

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
                 name: 'BDGRS',
36
                 name: 'BDGRS',
37
                 price: (500 + Math.floor(Math.random() * 100)).toString(),
37
                 price: (500 + Math.floor(Math.random() * 100)).toString(),
38
                 order: null,
38
                 order: null,
39
+                type: 'info',
39
             }
40
             }
40
 
41
 
41
             // Write to the input stream
42
             // Write to the input stream

+ 10
- 0
backend/lib/schemas/health.js Просмотреть файл

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 Просмотреть файл

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
 const Dotenv = require('dotenv').config({path: './server/.env'})
1
 const Dotenv = require('dotenv').config({path: './server/.env'})
2
-// const Dotenv = require('dotenv')
3
 const Confidence = require('@hapipal/confidence')
2
 const Confidence = require('@hapipal/confidence')
4
 const Inert = require('@hapi/inert')
3
 const Inert = require('@hapi/inert')
5
 const Vision = require('@hapi/vision')
4
 const Vision = require('@hapi/vision')
6
 const Schwifty = require('@hapipal/schwifty')
5
 const Schwifty = require('@hapipal/schwifty')
7
 const HapiSwagger = require('hapi-swagger')
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
 /** Glue manifest as a confidence store */
9
 /** Glue manifest as a confidence store */
14
 module.exports = new Confidence.Store({
10
 module.exports = new Confidence.Store({

+ 14
- 21
frontend/src/App.vue Просмотреть файл

24
 import 'wave-ui/dist/wave-ui.css'
24
 import 'wave-ui/dist/wave-ui.css'
25
 import SideBar from './components/SideBar.vue'
25
 import SideBar from './components/SideBar.vue'
26
 
26
 
27
-import { Chatter, currentProfile, StonkAlert } from './services'
27
+import { currentProfile } from './services'
28
 import { surveyFactory } from './utils'
28
 import { surveyFactory } from './utils'
29
 
29
 
30
 const DEV_MODE = import.meta.env.VITE_DEV == 'true'
30
 const DEV_MODE = import.meta.env.VITE_DEV == 'true'
52
         if (DEV_MODE) {
52
         if (DEV_MODE) {
53
             this.setPid(DEV_PID)
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
     methods: {
56
     methods: {
64
         /**
57
         /**
65
          * Sync up this components state with
58
          * Sync up this components state with
66
          * the currentProfile handler
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
 import { ref } from 'vue'
1
 import { ref } from 'vue'
2
-import { fetchResponsesByProfileId } from '../services'
2
+import { fetchResponsesByProfileId, Chatter, StonkAlert } from '../services'
3
 import { surveyFactory } from '../utils'
3
 import { surveyFactory } from '../utils'
4
 
4
 
5
 /**
5
 /**
9
 class Login {
9
 class Login {
10
     constructor() {
10
     constructor() {
11
         this._loading = false
11
         this._loading = false
12
-        
12
+
13
         // Make reactive with vue observer
13
         // Make reactive with vue observer
14
         this.id = ref(null)
14
         this.id = ref(null)
15
 
15
 
16
         this.responses = []
16
         this.responses = []
17
         this.tags = []
17
         this.tags = []
18
+
19
+        this.toaster = null
20
+        this.chatter = null
18
     }
21
     }
19
     get isLoading() {
22
     get isLoading() {
20
         return this._loading
23
         return this._loading
30
     }
33
     }
31
     /**
34
     /**
32
      * Combine questions retrieved from the database and
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
      * copare to responses stored
37
      * copare to responses stored
35
      * @returns {boolean}
38
      * @returns {boolean}
36
      */
39
      */
37
     get isComplete() {
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
      * Check that some responses are set
47
      * Check that some responses are set
43
      */
49
      */
44
     get hasResponses() {
50
     get hasResponses() {
45
         return this.responses.length && this.responses.length > 0
51
         return this.responses.length && this.responses.length > 0
46
-    } 
52
+    }
47
 
53
 
48
     /**
54
     /**
49
      * Login a profile by id number
55
      * Login a profile by id number
50
      * @param {number} profileId
56
      * @param {number} profileId
51
      * @returns {number} stored reactive id
57
      * @returns {number} stored reactive id
52
      */
58
      */
53
-     async login(profileId) {
59
+    async login(profileId) {
54
         console.warn('logging in:', profileId)
60
         console.warn('logging in:', profileId)
55
         this.id.value = parseInt(profileId)
61
         this.id.value = parseInt(profileId)
56
         return this.id.value
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
     async getTags() {
74
     async getTags() {
61
         try {
75
         try {
62
             const tags = []
76
             const tags = []
65
             console.error(err)
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
     async getResponses() {
86
     async getResponses() {
71
         try {
87
         try {
75
             console.error(err)
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
 const currentProfile = new Login()
110
 const currentProfile = new Login()

+ 11
- 5
frontend/src/services/notification.service.js Просмотреть файл

14
     listenFor(event, callback) {
14
     listenFor(event, callback) {
15
         this.source.addEventListener(event, callback)
15
         this.source.addEventListener(event, callback)
16
     }
16
     }
17
+    stop() {
18
+        this.source.close()
19
+    }
17
 }
20
 }
18
 
21
 
19
 /**
22
 /**
20
  * Example extension that listens for 'stonk' events
23
  * Example extension that listens for 'stonk' events
21
  */
24
  */
22
 class StonkAlert extends Toaster {
25
 class StonkAlert extends Toaster {
23
-    constructor(profileId) {
26
+    constructor(profileId, waveCb) {
24
         super(profileId)
27
         super(profileId)
25
-
28
+        this.event = 'stonk'
26
         this.stonks = {}
29
         this.stonks = {}
27
-        this.listenFor(`${profileId}.stonk`, message => {
30
+        this.listenFor(`${profileId}.${this.event}`, message => {
28
             const parsed = JSON.parse(message.data)
31
             const parsed = JSON.parse(message.data)
29
             this.stonks[parsed.name] = parsed
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 }

Загрузка…
Отмена
Сохранить