diff --git a/contacts/examples/create-new-contact.mjs b/contacts/examples/create-new-contact.mjs index 08a185e..f68a481 100644 --- a/contacts/examples/create-new-contact.mjs +++ b/contacts/examples/create-new-contact.mjs @@ -1,25 +1,29 @@ -import {ContactsModule} from '../dist/rdflib/index.js'; -import {Fetcher, graph, UpdateManager} from "rdflib"; -import {faker} from '@faker-js/faker'; +import { ContactsModule } from "../dist/rdflib/index.js"; +import { Fetcher, graph, UpdateManager } from "rdflib"; +import { faker } from "@faker-js/faker"; +const store = graph(); +const fetcher = new Fetcher(store); +const updater = new UpdateManager(store); +const contacts = new ContactsModule({ store, fetcher, updater }); -const store = graph() -const fetcher = new Fetcher(store) -const updater = new UpdateManager(store) -const contacts = new ContactsModule({store, fetcher, updater}) - -let addressBook = "http://localhost:3000/alice/public-write/ab9694d6-120e-415d-a315-90cd84c2e062/index.ttl#this"; +let addressBook = + "http://localhost:3000/alice/public-write/ab9694d6-120e-415d-a315-90cd84c2e062/index.ttl#this"; const firstName = faker.person.firstName(); const lastName = faker.person.lastName(); +const contact = { + name: firstName + " " + lastName, + email: faker.internet.email({ firstName, lastName }), + phoneNumber: faker.helpers.fromRegExp( + /[0-9]{2,4}-[0-9]{2,4}-[0-9]{2,4}-[0-9]{2,4}/, + ), +}; const uri = await contacts.createNewContact({ - addressBook: addressBook, - contact: { - name: firstName + " " + lastName, - email: faker.internet.email({firstName, lastName}) - } -}) -console.log("created contact:", firstName, lastName, uri) + addressBook: addressBook, + contact, +}); +console.log("created contact:", contact, "at", uri); -const result = await contacts.readAddressBook(addressBook) -console.log("the updated address book:", result) +const result = await contacts.readAddressBook(addressBook); +console.log("the updated address book:", result); diff --git a/contacts/src/rdflib/createNewContact.spec.ts b/contacts/src/rdflib/createNewContact.spec.ts index d773449..dce292f 100644 --- a/contacts/src/rdflib/createNewContact.spec.ts +++ b/contacts/src/rdflib/createNewContact.spec.ts @@ -131,6 +131,33 @@ describe("createNewContact", () => { ); }); + it("inserts the contact phone number to the contact document", () => { + const result = createNewContact(addressBookQuery, { + name: "Zinnia Lisa", + phoneNumber: "0118-999-881-99-9119-725-3", + }); + expect(result.insertions).toContainEqual( + st( + newContactNode, + vcard("hasTelephone"), + sym( + "https://pod.test/contacts/Person/80363534-f3d1-455e-a3d9-b29dcbb755d2/index.ttl#phone", + ), + newContactNode.doc(), + ), + ); + expect(result.insertions).toContainEqual( + st( + sym( + "https://pod.test/contacts/Person/80363534-f3d1-455e-a3d9-b29dcbb755d2/index.ttl#phone", + ), + vcard("value"), + sym("tel:0118-999-881-99-9119-725-3"), + newContactNode.doc(), + ), + ); + }); + it("does not insert an e-mail node if no e-mail is given", () => { const result = createNewContact(addressBookQuery, { name: "Zinnia Lisa", diff --git a/contacts/src/rdflib/createNewContact.ts b/contacts/src/rdflib/createNewContact.ts index 5bbb20e..ba4a372 100644 --- a/contacts/src/rdflib/createNewContact.ts +++ b/contacts/src/rdflib/createNewContact.ts @@ -14,7 +14,6 @@ export function createNewContact( if (!nameEmailIndex) { throw new Error("name-email index is missing or invalid"); } - const emailNode = sym(contactNode.doc().uri + "#email"); const insertions = [ st( contactNode, @@ -27,6 +26,8 @@ export function createNewContact( st(contactNode, rdf("type"), vcard("Individual"), contactNode.doc()), ]; if (newContact.email) { + const emailNode = sym(contactNode.doc().uri + "#email"); + insertions.push( st(contactNode, vcard("hasEmail"), emailNode, contactNode.doc()), st( @@ -37,6 +38,18 @@ export function createNewContact( ), ); } + if (newContact.phoneNumber) { + const phoneNode = sym(contactNode.doc().uri + "#phone"); + insertions.push( + st(contactNode, vcard("hasTelephone"), phoneNode, contactNode.doc()), + st( + phoneNode, + vcard("value"), + sym("tel:" + newContact.phoneNumber), + contactNode.doc(), + ), + ); + } return { uri: contactNode.uri, deletions: [],