@positivegrid/pg-mongoose-schema 27.8.5 → 28.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,107 +0,0 @@
1
- 'use strict'
2
-
3
- const _ = require('lodash')
4
- const moment = require('moment')
5
- const debug = require('debug')('model:usertrack')
6
-
7
- module.exports = function (mongoose) {
8
- const Schema = mongoose.Schema
9
- const ObjectId = Schema.Types.ObjectId
10
-
11
- const LICENSE_ADDITION = {
12
- PRO: 0,
13
- DEMO: 1,
14
- STD: 2,
15
- LITE: 3,
16
- ELITE: 4
17
- }
18
-
19
- const PRODUCTS = [
20
- 'fx2',
21
- 'amp2',
22
- 'fx1',
23
- 'amp1'
24
- ]
25
-
26
- const GenericDataSchema = new Schema({
27
- data_type: { type: String, required: true },
28
- data_key: { type: String, required: true },
29
- data_value: { type: Schema.Types.Mixed, required: true },
30
- data_meta: { type: Schema.Types.Mixed, default: null },
31
- created_on: { type: Date, default: Date.now }
32
- }, { collection: 'pg_generic_data' })
33
-
34
- const IapEventSchema = new Schema({
35
- user_id: { type: ObjectId, ref: 'User', required: true },
36
- app_name: { type: String, required: true },
37
- onesignal_id: { type: String, default: null },
38
- redeem_code: { type: ObjectId, default: null },
39
- created_on: { type: Date, default: Date.now },
40
- region: { type: String, required: false }
41
- }, { collection: 'pg_iap_event' })
42
-
43
- const DemoUserTrackerSchema = new Schema({
44
- user_id: { type: ObjectId, ref: 'User', required: true },
45
- product: { type: String, required: true },
46
- addition: { type: Number, required: true },
47
- started_at: { type: Date, required: true },
48
- updated_at: { type: Date, default: null },
49
- created_on: { type: Date, default: Date.now }
50
- }, { collection: 'demo_user_tracker' })
51
- DemoUserTrackerSchema.index({
52
- user_id: 1,
53
- product: 1,
54
- addition: 1,
55
- started_at: 1,
56
- updated_at: 1
57
- }, { unique: true })
58
-
59
- DemoUserTrackerSchema.static({
60
- saveTrackData: async function (data) {
61
- try {
62
- const { product, addition } = data
63
- const userID = mongoose.Types.ObjectId(data.userID)
64
- const startedAt = parseInt(data.startedAt, 10)
65
- console.log('!!', data, moment(startedAt), moment(startedAt).isValid(), mongoose.Types.ObjectId.isValid(userID), userID)
66
- if (!mongoose.Types.ObjectId.isValid(userID) ||
67
- PRODUCTS.indexOf(product) === -1 ||
68
- Object.keys(LICENSE_ADDITION).indexOf(addition) === -1) {
69
- throw Error(`Invalid parameter: ${userID}/${product}/${addition}`)
70
- } else if (
71
- !startedAt ||
72
- !moment(startedAt).isValid()
73
- ) {
74
- throw Error(`Invalid timestamp format: ${startedAt}/${updatedAt}`)
75
- }
76
-
77
- let trackData = {
78
- user_id: userID,
79
- product: product,
80
- addition: LICENSE_ADDITION[addition],
81
- started_at: moment(startedAt),
82
- }
83
-
84
- if (_.has(data, 'updatedAt')) {
85
- const updatedAt = parseInt(data.updatedAt, 10)
86
- if (updatedAt && moment(updatedAt).isValid()) {
87
- trackData['updated_at'] = moment(updatedAt)
88
- }
89
- }
90
-
91
- const ret = await this.findOne(trackData).exec()
92
- if (!ret) {
93
- await this.create(trackData)
94
- }
95
- return true
96
- if (ret) { return }
97
- } catch (err) {
98
- debug('ERR:handleLicensePurchaseData:%j', err)
99
- throw err
100
- }
101
- }
102
- })
103
-
104
- mongoose.model('GenericData', GenericDataSchema)
105
- mongoose.model('IapEvent', IapEventSchema)
106
- mongoose.model('DemoUserTracker', DemoUserTrackerSchema)
107
- }