Skip to content

Commit

Permalink
contacts: add e-mail address 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 b6b4dc3 commit 1c244a3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
8 changes: 5 additions & 3 deletions contacts/examples/create-new-contact.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ const contacts = new ContactsModule({store, fetcher, updater})

let addressBook = "http://localhost:3000/alice/public-write/ab9694d6-120e-415d-a315-90cd84c2e062/index.ttl#this";

const name = faker.person.fullName();
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const uri = await contacts.createNewContact({
addressBook: addressBook,
contact: {
name: name
name: firstName + " " + lastName,
email: faker.internet.email({firstName, lastName})
}
})
console.log("created contact:", name, uri)
console.log("created contact:", firstName, lastName, uri)

const result = await contacts.readAddressBook(addressBook)
console.log("the updated address book:", result)
43 changes: 43 additions & 0 deletions contacts/src/rdflib/createNewContact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,49 @@ describe("createNewContact", () => {
);
});

it("inserts the contact e-mail to the contact document", () => {
const result = createNewContact(addressBookQuery, {
name: "Zinnia Lisa",
email: "zinnia.lisa@mail.test",
});
expect(result.insertions).toContainEqual(
st(
newContactNode,
vcard("hasEmail"),
sym(
"https://pod.test/contacts/Person/80363534-f3d1-455e-a3d9-b29dcbb755d2/index.ttl#email",
),
newContactNode.doc(),
),
);
expect(result.insertions).toContainEqual(
st(
sym(
"https://pod.test/contacts/Person/80363534-f3d1-455e-a3d9-b29dcbb755d2/index.ttl#email",
),
vcard("value"),
sym("mailto:zinnia.lisa@mail.test"),
newContactNode.doc(),
),
);
});

it("does not insert an e-mail node if no e-mail is given", () => {
const result = createNewContact(addressBookQuery, {
name: "Zinnia Lisa",
});
expect(result.insertions).not.toContainEqual(
st(
newContactNode,
vcard("hasEmail"),
sym(
"https://pod.test/contacts/Person/80363534-f3d1-455e-a3d9-b29dcbb755d2/index.ttl#email",
),
newContactNode.doc(),
),
);
});

it("adds a type to the new contact", () => {
const result = createNewContact(addressBookQuery, {
name: "anyone",
Expand Down
37 changes: 25 additions & 12 deletions contacts/src/rdflib/createNewContact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UpdateOperation } from "./web-operations/executeUpdate";
import { AddressBookQuery } from "./AddressBookQuery";
import { lit, st } from "rdflib";
import { lit, st, sym } from "rdflib";
import { rdf, vcard } from "./namespaces";

import { NewContact } from "../index";
Expand All @@ -14,20 +14,33 @@ 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,
vcard("inAddressBook"),
addressBook.addressBookNode,
nameEmailIndex,
),
st(contactNode, vcard("fn"), lit(newContact.name), nameEmailIndex),
st(contactNode, vcard("fn"), lit(newContact.name), contactNode.doc()),
st(contactNode, rdf("type"), vcard("Individual"), contactNode.doc()),
];
if (newContact.email) {
insertions.push(
st(contactNode, vcard("hasEmail"), emailNode, contactNode.doc()),
st(
emailNode,
vcard("value"),
sym("mailto:" + newContact.email),
contactNode.doc(),
),
);
}
return {
uri: contactNode.uri,
deletions: [],
insertions: [
st(
contactNode,
vcard("inAddressBook"),
addressBook.addressBookNode,
nameEmailIndex,
),
st(contactNode, vcard("fn"), lit(newContact.name), nameEmailIndex),
st(contactNode, vcard("fn"), lit(newContact.name), contactNode.doc()),
st(contactNode, rdf("type"), vcard("Individual"), contactNode.doc()),
],
insertions: insertions,
filesToCreate: [],
};
}

0 comments on commit 1c244a3

Please sign in to comment.