-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.ts
More file actions
100 lines (94 loc) · 2.85 KB
/
Copy pathPerson.ts
File metadata and controls
100 lines (94 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { appInstance } from '@adaptivestone/framework/helpers/appInstance.js';
import type {
GetModelTypeFromClass,
GetModelTypeLiteFromSchema,
} from '@adaptivestone/framework/modules/BaseModel.js';
import { BaseModel } from '@adaptivestone/framework/modules/BaseModel.js';
import Mailer from '@adaptivestone/framework-module-email';
import type { TFunction } from 'i18next';
export type TPerson = GetModelTypeFromClass<typeof Person>;
type PersonAuthoringModel = GetModelTypeLiteFromSchema<
typeof Person.modelSchema
>;
type PersonAuthoringDocument = InstanceType<PersonAuthoringModel>;
class Person extends BaseModel {
static get modelSchema() {
return {
firstName: {
type: String,
required: [true, 'person.firstNameRequired'],
},
lastName: String,
email: String,
labels: { type: [String] },
} as const;
}
/**
* const Person = appInstance.getModel('Person');
* const person = await SomeModel.findOne({email:"cfff"});
* const data = await person.sendCreatEmail({t, 'en'}); // call instance method
*/
static get modelInstanceMethods() {
return {
sendCreatEmail: async function sendCreatEmail(
this: PersonAuthoringDocument,
i18n: { t: TFunction; language: string },
) {
const mail = new Mailer(
appInstance,
'verification',
{
fullName: `${this.firstName} ${this.lastName}`,
},
i18n,
);
this.email = 'test@example.com';
return mail.send(this.email);
},
} as const;
}
/**
* Object with static methods.
* this.app.getModel('SomeModel').findByEmail('email');
* this.app.getModel('SomeModel').getInfoStatic();
*
*/
static get modelStatics() {
return {
findByFullName: async function findByFullName(
// Schema-derived model context avoids a circular reference while this
// class's custom members are still being inferred.
this: PersonAuthoringModel,
fullName: string,
) {
const firstSpace = fullName.indexOf(' ');
const firstName = fullName.split(' ')[0];
const lastName =
firstSpace === -1 ? '' : fullName.substring(firstSpace + 1);
return this.findOne({ firstName, lastName });
},
};
}
// virtual field
static get modelVirtuals() {
return {
fullName: {
// schema
options: {
type: String,
} as const,
// Getter
get(this: PersonAuthoringDocument) {
return `${this.firstName} ${this.lastName}`;
},
// Setter
async set(this: PersonAuthoringDocument, v: string) {
const firstName = v.substring(0, v.indexOf(' '));
const lastName = v.substring(v.indexOf(' ') + 1);
this.set({ firstName, lastName });
},
},
};
}
}
export default Person;