Skip to content

(feat): updated contact model with accord PhoneNumber and Email types #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/dataModel/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
concerto version "^3.0.0"
namespace [email protected]

/*
* Definitions in this file do not enforce character constraints (for example, using
* regular expressions or digits). It is the responsibility of the application to enforce
* theseconstraints. This allows broader compatibility between systems and agreements which
* may have different requirements.
*/

/*
* An email address without enclosing angle brackets
*
* Note: There is a restriction in RFC 2821 on the length of an
* address in MAIL and RCPT commands of 254 characters
* https://www.rfc-editor.org/errata_search.php?rfc=3696
*/
scalar EmailAddress extends String length=[,254]

/*
* A representation of telephone numbers to define commonly used structures
*/
concept PhoneNumber {
/*
* A formatted E.123 national number, potentially including spaces parentheses
* and separators (e.g. '-'). Does not include an International Prefix, extension,
* trunk prefix or alternate endings.
* https://www.itu.int/rec/T-REC-E.123-200102-I/en
*/
o String number length=[0,32]

/*
* A standardized E.164 international telephone number without formatting such
* as spaces, parentheses or separators (e.g. '-'). Includes the International Prefix
* and Subscriber Number (National Destination Code and Base Number). Does not
* include an extension, trunk prefix or alternate endings.
* https://www.itu.int/rec/T-REC-E.164-201011-I/en
*/
o String normalizedNumber length=[,15] optional

/*
* An E.164 Network Address Extension / Sub-address ("Ext.", Direct Dial In,
* or Direct Inward Dial) number. Does not include a "Ext." characters.
* https://www.itu.int/rec/T-REC-E.164-201011-I/en
*/
o String extension length=[0,6] optional

/*
* The International Prefix or Country Code (CC) for the telephone number. Includes
* the leading '+' character or zeros. The International Prefix has 1-3 digits.
* https://www.itu.int/rec/T-REC-E.164-201011-I/en
*/
o String countryCode length=[0,7] optional
}
5 changes: 3 additions & 2 deletions src/dataModel/model.cto
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace [email protected]
import [email protected].{EmailAddress, PhoneNumber}

enum AccountType_Enum {
o Prospect
Expand Down Expand Up @@ -165,11 +166,11 @@ concept Contact identified by Id {

@Term("Email")
@Crud("Createable,Readable,Updateable")
o String email optional
o EmailAddress email optional

@Term("Phone")
@Crud("Createable,Readable,Updateable")
o String phone optional
o PhoneNumber phone optional

@Term("Opportunity Link")
@Crud("Createable,Readable,Updateable")
Expand Down
11 changes: 9 additions & 2 deletions src/db/Contact.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
"Id": "1",
"fullName": "Sarah Johnson",
"email": "[email protected]",
"phone": "+1-415-555-0193",
"phone": {
"number":"4155550193",
"countryCode":"1",
"extension":"999"
},
"currentOpportunity": "1"
},
{
"Id": "2",
"fullName": "Michael Chen",
"email": "[email protected]",
"phone": "+1-510-555-0176",
"phone": {
"number":"5105550176",
"countryCode":"1"
},
"currentOpportunity": "2"
}
]
Expand Down
27 changes: 21 additions & 6 deletions src/services/dataio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IReq, IRes } from '../utils/types';
import { QueryExecutor } from '../utils/queryExecutor';
import { FileDB } from '../db/fileDB';
import moment from 'moment';
import { ConceptDeclaration, ModelManager } from '@accordproject/concerto-core';
import { ConceptDeclaration, EnumDeclaration, MapDeclaration, ModelManager, ScalarDeclaration } from '@accordproject/concerto-core';
import path from 'path';
import { ModelManagerUtil } from '../utils/modelManagerUtil';
import { ResultRehydrator } from '../utils/resultRehydrator';
Expand Down Expand Up @@ -120,8 +120,8 @@ const generateErrorResponse = (message: string, code: string): ErrorResponse =>
* Concerto model manager setup using CTO file.
* Model manager allowes users to load in CTO files and use Concerto model features directly in code.
*/
const MODEL_MANAGER: ModelManager = ModelManagerUtil.createModelManagerFromCTO(path.join(__dirname, "../dataModel/model.cto"));
const CONCEPTS: ConceptDeclaration[] = MODEL_MANAGER.getConceptDeclarations();
const MODEL_MANAGER: ModelManager = ModelManagerUtil.createModelManagerFromCTO();
const CONCEPTS: ConceptDeclaration[] = MODEL_MANAGER.getModelFile("[email protected]").getConceptDeclarations();
const READABLE_CONCEPTS: ConceptDeclaration[] = CONCEPTS.filter(isReadableConcept);

/**
Expand Down Expand Up @@ -247,11 +247,26 @@ export const getTypeDefinitions = (req: IReq<GetTypeDefinitionsBody>, res: IRes)
if (!typeNames) {
return res.status(400).json(generateErrorResponse(ErrorCode.BAD_REQUEST, 'Missing typeNames in request')).send();
}
MODEL_MANAGER.addCTOModel

try {
const modelFile = MODEL_MANAGER.getModelFile("[email protected]");

return res.json({
declarations: READABLE_CONCEPTS.map((concept: ConceptDeclaration) => concept.ast)
})
declarations: [
...modelFile
.getEnumDeclarations()
.map((decl: EnumDeclaration) => decl.ast),
...modelFile
.getScalarDeclarations()
.map((decl: ScalarDeclaration) => decl.ast),
...modelFile
.getConceptDeclarations()
.map((decl: ConceptDeclaration) => decl.ast),
...modelFile
.getMapDeclarations()
.map((decl: MapDeclaration) => decl.value.ast)
]
});
} catch (err) {
console.log(`Encountered an error getting type definitions: ${err.message}`);
return res.status(500).json(generateErrorResponse(ErrorCode.INTERNAL_ERROR, err)).send();
Expand Down
24 changes: 19 additions & 5 deletions src/utils/modelManagerUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@ import path from "path";
import fs from 'fs';

export class ModelManagerUtil {
private static DATA_MODELS_FOLDER: string = '../dataModel/';
private static DATA_MODELS_FILES: string[] = [
'[email protected]',
'model.cto',
];

/**
* Create a ModelManager instance from a CTO file.
* @param {string} ctoFile - the path to the CTO file.
* @return {ModelManager} a ModelManager instance.
*/
public static createModelManagerFromCTO(ctoFile: string): ModelManager {
public static createModelManagerFromCTO(): ModelManager {
// Needed as this is a workaround to skip line locations in generated AST
// @ts-ignore
const modelManager: ModelManager = new ModelManager({ strict: true, skipLocationNodes: true });
modelManager.addCTOModel(fs.readFileSync(path.join(__dirname, "../dataModel/model.cto"),'utf8'));
const modelManager: ModelManager = new ModelManager({ strict: true,skipLocationNodes: true });

this.DATA_MODELS_FILES.forEach((fileName: string) => {
modelManager.addCTOModel(
fs.readFileSync(
path.join(__dirname, `${this.DATA_MODELS_FOLDER}${fileName}`),
'utf8'
)
);
});

modelManager.validateModelFiles();
return modelManager
return modelManager;
}
}