Skip to content

Commit

Permalink
contacts: add phone number to new contact
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo-v committed Dec 13, 2023
1 parent 1c244a3 commit 09e9b65
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
40 changes: 22 additions & 18 deletions contacts/examples/create-new-contact.mjs
Original file line number Diff line number Diff line change
@@ -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);
27 changes: 27 additions & 0 deletions contacts/src/rdflib/createNewContact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 14 additions & 1 deletion contacts/src/rdflib/createNewContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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: [],
Expand Down

0 comments on commit 09e9b65

Please sign in to comment.