@htilabs/platform-common-models 0.1.0-beta.0

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.
Files changed (48) hide show
  1. package/dist/constants/index.cjs +1379 -0
  2. package/dist/constants/index.cjs.map +1 -0
  3. package/dist/constants/index.d.cts +528 -0
  4. package/dist/constants/index.d.ts +528 -0
  5. package/dist/constants/index.js +1252 -0
  6. package/dist/constants/index.js.map +1 -0
  7. package/dist/index.cjs +2138 -0
  8. package/dist/index.cjs.map +1 -0
  9. package/dist/index.d.cts +5 -0
  10. package/dist/index.d.ts +5 -0
  11. package/dist/index.js +1986 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/models/index.cjs +661 -0
  14. package/dist/models/index.cjs.map +1 -0
  15. package/dist/models/index.d.cts +204 -0
  16. package/dist/models/index.d.ts +204 -0
  17. package/dist/models/index.js +636 -0
  18. package/dist/models/index.js.map +1 -0
  19. package/dist/schemas/index.cjs +1405 -0
  20. package/dist/schemas/index.cjs.map +1 -0
  21. package/dist/schemas/index.d.cts +312 -0
  22. package/dist/schemas/index.d.ts +312 -0
  23. package/dist/schemas/index.js +1353 -0
  24. package/dist/schemas/index.js.map +1 -0
  25. package/dist/scoring-rule-BOkG46I3.d.cts +1179 -0
  26. package/dist/scoring-rule-BOkG46I3.d.ts +1179 -0
  27. package/dist/types/index.cjs +19 -0
  28. package/dist/types/index.cjs.map +1 -0
  29. package/dist/types/index.d.cts +1 -0
  30. package/dist/types/index.d.ts +1 -0
  31. package/dist/types/index.js +1 -0
  32. package/dist/types/index.js.map +1 -0
  33. package/package.json +81 -0
  34. package/src/constants/ic.ts +1630 -0
  35. package/src/constants/index.ts +11 -0
  36. package/src/index.ts +30 -0
  37. package/src/models/formModel.ts +30 -0
  38. package/src/models/index.ts +72 -0
  39. package/src/models/questionModel.ts +30 -0
  40. package/src/models/scoringRuleModel.ts +33 -0
  41. package/src/models/sectionModel.ts +30 -0
  42. package/src/schemas/common.ts +118 -0
  43. package/src/schemas/form.ts +125 -0
  44. package/src/schemas/index.ts +70 -0
  45. package/src/schemas/question.ts +297 -0
  46. package/src/schemas/scoring-rule.ts +169 -0
  47. package/src/schemas/section.ts +158 -0
  48. package/src/types/index.ts +47 -0
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @htilabs/platform-common-models - Constants
3
+ *
4
+ * Shared constants for HTI Labs applications including:
5
+ * - Information Concept (IC) codes for victim services data standards
6
+ * - Data quality indicators
7
+ * - Person types and involvement roles
8
+ * - Form configuration constants
9
+ */
10
+
11
+ export * from "./ic.js";
package/src/index.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @htilabs/platform-common-models
3
+ *
4
+ * Shared constants, types, schemas, and models for HTI Labs applications.
5
+ * Provides Information Concept (IC) codes, data quality indicators,
6
+ * Mongoose schemas, and model factories for PAVE, Thread, and other platforms.
7
+ *
8
+ * @example Constants only (no mongoose required)
9
+ * ```typescript
10
+ * import { IC_5_VICTIMIZATION, DATA_QUALITY } from '@htilabs/platform-common-models';
11
+ * ```
12
+ *
13
+ * @example With models (mongoose required)
14
+ * ```typescript
15
+ * import { createPlatformModels } from '@htilabs/platform-common-models/models';
16
+ * import mongoose from 'mongoose';
17
+ *
18
+ * const platformDb = mongoose.createConnection(PLATFORM_DB_URI);
19
+ * const { FormModel, SectionModel, QuestionModel } = createPlatformModels(platformDb);
20
+ * ```
21
+ */
22
+
23
+ // Export all constants
24
+ export * from "./constants/index.js";
25
+
26
+ // Export schemas (requires mongoose peer dependency)
27
+ export * from "./schemas/index.js";
28
+
29
+ // Export model factories (requires mongoose peer dependency)
30
+ export * from "./models/index.js";
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @htilabs/platform-common-models — Form Model
3
+ *
4
+ * Factory for creating Form model bound to a specific connection.
5
+ * Follows the platform-common-services pattern with buildSchema/compileModel.
6
+ */
7
+
8
+ import type { Connection, Model, HydratedDocument } from "mongoose";
9
+ import { buildFormSchema, type Form } from "../schemas/form.js";
10
+
11
+ export type FormDoc = HydratedDocument<Form>;
12
+
13
+ /**
14
+ * Build the Form schema.
15
+ * @returns Mongoose Schema for Form
16
+ */
17
+ export { buildFormSchema as buildSchema };
18
+
19
+ /**
20
+ * Compile the Form model on a specific connection.
21
+ * @param connection - Mongoose connection to bind the model to
22
+ * @param schema - Optional pre-built schema (uses buildFormSchema() if not provided)
23
+ * @returns Mongoose Model for Form
24
+ */
25
+ export function compileModel(
26
+ connection: Connection,
27
+ schema = buildFormSchema(),
28
+ ): Model<Form> {
29
+ return connection.model<Form>("Form", schema);
30
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @htilabs/platform-common-models — Models
3
+ *
4
+ * Mongoose model factories for platform models.
5
+ * Each model exports buildSchema() and compileModel(connection) functions.
6
+ *
7
+ * Usage:
8
+ * ```typescript
9
+ * import { createPlatformModels } from '@htilabs/platform-common-models/models';
10
+ * import mongoose from 'mongoose';
11
+ *
12
+ * const platformDb = mongoose.createConnection(PLATFORM_DB_URI);
13
+ * const { FormModel, SectionModel, QuestionModel, ScoringRuleModel } = createPlatformModels(platformDb);
14
+ * ```
15
+ */
16
+
17
+ import type { Connection, Model } from "mongoose";
18
+
19
+ // Individual model factories
20
+ import * as formModel from "./formModel.js";
21
+ import * as sectionModel from "./sectionModel.js";
22
+ import * as questionModel from "./questionModel.js";
23
+ import * as scoringRuleModel from "./scoringRuleModel.js";
24
+
25
+ // Re-export individual model factories
26
+ export { formModel, sectionModel, questionModel, scoringRuleModel };
27
+
28
+ // Re-export types
29
+ export type { FormDoc } from "./formModel.js";
30
+ export type { SectionDoc } from "./sectionModel.js";
31
+ export type { QuestionDoc } from "./questionModel.js";
32
+ export type { ScoringRuleDoc } from "./scoringRuleModel.js";
33
+
34
+ // Re-export schema types
35
+ export type { Form } from "../schemas/form.js";
36
+ export type { Section } from "../schemas/section.js";
37
+ export type { Question } from "../schemas/question.js";
38
+ export type { ScoringRule } from "../schemas/scoring-rule.js";
39
+
40
+ /**
41
+ * Platform models interface returned by createPlatformModels.
42
+ */
43
+ export interface PlatformModels {
44
+ FormModel: Model<import("../schemas/form.js").Form>;
45
+ SectionModel: Model<import("../schemas/section.js").Section>;
46
+ QuestionModel: Model<import("../schemas/question.js").Question>;
47
+ ScoringRuleModel: Model<import("../schemas/scoring-rule.js").ScoringRule>;
48
+ }
49
+
50
+ /**
51
+ * Create all platform models bound to a single connection.
52
+ * Convenience function for apps that need all form-related models.
53
+ *
54
+ * @param connection - Mongoose connection to bind all models to
55
+ * @returns Object with all compiled models
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const platformDb = mongoose.createConnection(PLATFORM_DB_URI);
60
+ * const { FormModel, SectionModel, QuestionModel, ScoringRuleModel } = createPlatformModels(platformDb);
61
+ *
62
+ * const forms = await FormModel.find({ status: 'active' });
63
+ * ```
64
+ */
65
+ export function createPlatformModels(connection: Connection): PlatformModels {
66
+ return {
67
+ FormModel: formModel.compileModel(connection),
68
+ SectionModel: sectionModel.compileModel(connection),
69
+ QuestionModel: questionModel.compileModel(connection),
70
+ ScoringRuleModel: scoringRuleModel.compileModel(connection),
71
+ };
72
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @htilabs/platform-common-models — Question Model
3
+ *
4
+ * Factory for creating Question model bound to a specific connection.
5
+ * Follows the platform-common-services pattern with buildSchema/compileModel.
6
+ */
7
+
8
+ import type { Connection, Model, HydratedDocument } from "mongoose";
9
+ import { buildQuestionSchema, type Question } from "../schemas/question.js";
10
+
11
+ export type QuestionDoc = HydratedDocument<Question>;
12
+
13
+ /**
14
+ * Build the Question schema.
15
+ * @returns Mongoose Schema for Question
16
+ */
17
+ export { buildQuestionSchema as buildSchema };
18
+
19
+ /**
20
+ * Compile the Question model on a specific connection.
21
+ * @param connection - Mongoose connection to bind the model to
22
+ * @param schema - Optional pre-built schema (uses buildQuestionSchema() if not provided)
23
+ * @returns Mongoose Model for Question
24
+ */
25
+ export function compileModel(
26
+ connection: Connection,
27
+ schema = buildQuestionSchema(),
28
+ ): Model<Question> {
29
+ return connection.model<Question>("Question", schema);
30
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @htilabs/platform-common-models — ScoringRule Model
3
+ *
4
+ * Factory for creating ScoringRule model bound to a specific connection.
5
+ * Follows the platform-common-services pattern with buildSchema/compileModel.
6
+ */
7
+
8
+ import type { Connection, Model, HydratedDocument } from "mongoose";
9
+ import {
10
+ buildScoringRuleSchema,
11
+ type ScoringRule,
12
+ } from "../schemas/scoring-rule.js";
13
+
14
+ export type ScoringRuleDoc = HydratedDocument<ScoringRule>;
15
+
16
+ /**
17
+ * Build the ScoringRule schema.
18
+ * @returns Mongoose Schema for ScoringRule
19
+ */
20
+ export { buildScoringRuleSchema as buildSchema };
21
+
22
+ /**
23
+ * Compile the ScoringRule model on a specific connection.
24
+ * @param connection - Mongoose connection to bind the model to
25
+ * @param schema - Optional pre-built schema (uses buildScoringRuleSchema() if not provided)
26
+ * @returns Mongoose Model for ScoringRule
27
+ */
28
+ export function compileModel(
29
+ connection: Connection,
30
+ schema = buildScoringRuleSchema(),
31
+ ): Model<ScoringRule> {
32
+ return connection.model<ScoringRule>("ScoringRule", schema);
33
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @htilabs/platform-common-models — Section Model
3
+ *
4
+ * Factory for creating Section model bound to a specific connection.
5
+ * Follows the platform-common-services pattern with buildSchema/compileModel.
6
+ */
7
+
8
+ import type { Connection, Model, HydratedDocument } from "mongoose";
9
+ import { buildSectionSchema, type Section } from "../schemas/section.js";
10
+
11
+ export type SectionDoc = HydratedDocument<Section>;
12
+
13
+ /**
14
+ * Build the Section schema.
15
+ * @returns Mongoose Schema for Section
16
+ */
17
+ export { buildSectionSchema as buildSchema };
18
+
19
+ /**
20
+ * Compile the Section model on a specific connection.
21
+ * @param connection - Mongoose connection to bind the model to
22
+ * @param schema - Optional pre-built schema (uses buildSectionSchema() if not provided)
23
+ * @returns Mongoose Model for Section
24
+ */
25
+ export function compileModel(
26
+ connection: Connection,
27
+ schema = buildSectionSchema(),
28
+ ): Model<Section> {
29
+ return connection.model<Section>("Section", schema);
30
+ }
@@ -0,0 +1,118 @@
1
+ /**
2
+ * @htilabs/platform-common-models — Common Sub-Schemas
3
+ *
4
+ * Reusable sub-schemas for embedded documents across platform models.
5
+ * All sub-schemas are defined with { _id: false } for clean embedding.
6
+ */
7
+
8
+ import { Schema } from "mongoose";
9
+ import type { InferRawDocType } from "mongoose";
10
+ import { DATA_QUALITY_VALUES } from "../constants/ic.js";
11
+
12
+ // ─── Address Sub-Schema ──────────────────────────────────────────────────────
13
+
14
+ export const addressSchema = new Schema(
15
+ {
16
+ formattedAddress: String,
17
+ street: String,
18
+ city: String,
19
+ county: String,
20
+ state: String,
21
+ zip: String,
22
+ country: String,
23
+ lat: Number,
24
+ lng: Number,
25
+ placeId: String,
26
+ },
27
+ { _id: false },
28
+ );
29
+
30
+ export type Address = InferRawDocType<typeof addressSchema>;
31
+
32
+ // ─── Person Name Sub-Schema ──────────────────────────────────────────────────
33
+
34
+ export const personNameSchema = new Schema(
35
+ {
36
+ firstName: String,
37
+ lastName: String,
38
+ middleName: String,
39
+ suffix: String,
40
+ nameType: String,
41
+ isPrimary: Boolean,
42
+ alias: Boolean,
43
+ isUnknown: Boolean,
44
+ isAnonymous: Boolean,
45
+ },
46
+ { _id: false },
47
+ );
48
+
49
+ export type PersonName = InferRawDocType<typeof personNameSchema>;
50
+
51
+ // ─── Phone Sub-Schema ────────────────────────────────────────────────────────
52
+
53
+ export const phoneSchema = new Schema(
54
+ {
55
+ number: String,
56
+ type: String,
57
+ safeToContact: Boolean,
58
+ dataQuality: { type: String, enum: DATA_QUALITY_VALUES },
59
+ },
60
+ { _id: false },
61
+ );
62
+
63
+ export type Phone = InferRawDocType<typeof phoneSchema>;
64
+
65
+ // ─── Email Sub-Schema ────────────────────────────────────────────────────────
66
+
67
+ export const emailSchema = new Schema(
68
+ {
69
+ address: String,
70
+ type: String,
71
+ safeToContact: Boolean,
72
+ },
73
+ { _id: false },
74
+ );
75
+
76
+ export type Email = InferRawDocType<typeof emailSchema>;
77
+
78
+ // ─── Demographics Sub-Schema ─────────────────────────────────────────────────
79
+
80
+ export const demographicsSchema = new Schema(
81
+ {
82
+ // Age / DOB fields
83
+ age: Number,
84
+ dateOfBirth: String,
85
+ dobMonth: String,
86
+ dobDay: String,
87
+ dobYear: String,
88
+ ageAtContact: Number,
89
+ ageBin: String,
90
+ apparentAge: String,
91
+ ageType: Number,
92
+ minorAdultStatus: Number,
93
+
94
+ // Demographics value fields (IC-22, IC-23, IC-24, IC-25)
95
+ sex: Number,
96
+ genderIdentity: Number,
97
+ race: [Number],
98
+ ethnicity: [Number],
99
+
100
+ // Free-text detail for race/gender
101
+ raceOtherText: String,
102
+ genderOtherText: String,
103
+
104
+ // Special accommodations
105
+ accommodations: [String],
106
+ accommodationOtherText: String,
107
+
108
+ // Per-field data quality indicators (HMIS-aligned)
109
+ ageDataQuality: { type: String, enum: DATA_QUALITY_VALUES },
110
+ sexDataQuality: { type: String, enum: DATA_QUALITY_VALUES },
111
+ genderDataQuality: { type: String, enum: DATA_QUALITY_VALUES },
112
+ raceDataQuality: { type: String, enum: DATA_QUALITY_VALUES },
113
+ ethnicityDataQuality: { type: String, enum: DATA_QUALITY_VALUES },
114
+ },
115
+ { _id: false },
116
+ );
117
+
118
+ export type Demographics = InferRawDocType<typeof demographicsSchema>;
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @htilabs/platform-common-models — Form Schema
3
+ *
4
+ * Versioned form definitions for screening, intake, and assessment forms.
5
+ * Forms contain ordered references to sections with display metadata.
6
+ */
7
+
8
+ import { Schema } from "mongoose";
9
+ import type { InferRawDocType } from "mongoose";
10
+
11
+ // ─── Form Section Reference Sub-Schema ───────────────────────────────────────
12
+
13
+ export const formSectionRefSchema = new Schema(
14
+ {
15
+ sectionId: { type: String, required: true },
16
+ sectionVersion: { type: String, required: true },
17
+ index: { type: Number, required: true },
18
+ isStep: { type: Boolean, default: false },
19
+ wizardStep: { type: Number },
20
+ label: { type: String },
21
+ presetsType: { type: String },
22
+ },
23
+ { _id: false },
24
+ );
25
+
26
+ export type FormSectionRef = InferRawDocType<typeof formSectionRefSchema>;
27
+
28
+ // ─── Form Schema ─────────────────────────────────────────────────────────────
29
+
30
+ export const formSchemaDefinition = {
31
+ _schemaVersion: {
32
+ type: Number,
33
+ required: true,
34
+ default: 1,
35
+ },
36
+
37
+ // Audit fields
38
+ createdUser: {
39
+ type: Schema.Types.ObjectId,
40
+ required: true,
41
+ index: true,
42
+ },
43
+ updatedUser: {
44
+ type: Schema.Types.ObjectId,
45
+ index: true,
46
+ },
47
+
48
+ // Versioning
49
+ key: {
50
+ type: String,
51
+ required: true,
52
+ },
53
+ version: {
54
+ type: String,
55
+ required: true,
56
+ },
57
+
58
+ // Form info
59
+ name: {
60
+ type: String,
61
+ required: true,
62
+ },
63
+ description: {
64
+ type: String,
65
+ },
66
+ formType: {
67
+ type: String,
68
+ index: true,
69
+ },
70
+ appScope: {
71
+ type: [String],
72
+ enum: ["thread", "pave", "both"],
73
+ default: [],
74
+ },
75
+
76
+ // Ordered sections
77
+ sections: {
78
+ type: [formSectionRefSchema],
79
+ default: [],
80
+ },
81
+
82
+ // Lifecycle
83
+ status: {
84
+ type: String,
85
+ required: true,
86
+ enum: ["draft", "active", "deprecated"],
87
+ default: "draft",
88
+ index: true,
89
+ },
90
+ supersedes: {
91
+ type: String,
92
+ },
93
+ };
94
+
95
+ export const formSchemaOptions = {
96
+ timestamps: {
97
+ createdAt: "createdTime",
98
+ updatedAt: "updatedTime",
99
+ },
100
+ collection: "forms",
101
+ };
102
+
103
+ /**
104
+ * Build the Form schema.
105
+ * @returns Mongoose Schema for Form
106
+ */
107
+ export function buildFormSchema(): Schema {
108
+ const schema = new Schema(formSchemaDefinition, formSchemaOptions);
109
+
110
+ // Compound unique: only one version of a given key
111
+ schema.index({ key: 1, version: 1 }, { unique: true });
112
+
113
+ // Partial unique: only one "active" document per key
114
+ schema.index(
115
+ { key: 1 },
116
+ { unique: true, partialFilterExpression: { status: "active" } },
117
+ );
118
+
119
+ return schema;
120
+ }
121
+
122
+ // Pre-built schema for type inference
123
+ const _formSchema = buildFormSchema();
124
+
125
+ export type Form = InferRawDocType<typeof _formSchema>;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * @htilabs/platform-common-models — Schemas
3
+ *
4
+ * Mongoose schema definitions for platform models.
5
+ * Use buildXxxSchema() functions to create schemas, then bind to your connection.
6
+ */
7
+
8
+ // Common sub-schemas
9
+ export {
10
+ addressSchema,
11
+ personNameSchema,
12
+ phoneSchema,
13
+ emailSchema,
14
+ demographicsSchema,
15
+ type Address,
16
+ type PersonName,
17
+ type Phone,
18
+ type Email,
19
+ type Demographics,
20
+ } from "./common.js";
21
+
22
+ // Form schema
23
+ export {
24
+ formSectionRefSchema,
25
+ buildFormSchema,
26
+ formSchemaDefinition,
27
+ formSchemaOptions,
28
+ type FormSectionRef,
29
+ type Form,
30
+ } from "./form.js";
31
+
32
+ // Section schema
33
+ export {
34
+ sectionQuestionRefSchema,
35
+ guidanceSchema,
36
+ buildSectionSchema,
37
+ sectionSchemaDefinition,
38
+ sectionSchemaOptions,
39
+ type SectionQuestionRef,
40
+ type Guidance,
41
+ type Section,
42
+ } from "./section.js";
43
+
44
+ // Question schema
45
+ export {
46
+ choiceSchema,
47
+ visibilityRuleSchema,
48
+ layoutSchema,
49
+ buildQuestionSchema,
50
+ questionSchemaDefinition,
51
+ questionSchemaOptions,
52
+ type Choice,
53
+ type VisibilityRule,
54
+ type Layout,
55
+ type Question,
56
+ } from "./question.js";
57
+
58
+ // Scoring rule schema
59
+ export {
60
+ scoringConditionSchema,
61
+ scoringThresholdSchema,
62
+ scoringRecommendationSchema,
63
+ buildScoringRuleSchema,
64
+ scoringRuleSchemaDefinition,
65
+ scoringRuleSchemaOptions,
66
+ type ScoringCondition,
67
+ type ScoringThreshold,
68
+ type ScoringRecommendation,
69
+ type ScoringRule,
70
+ } from "./scoring-rule.js";