Skip to content

Commit

Permalink
refactor: Add doctypes constantes
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed Sep 13, 2024
1 parent 5967d1e commit 77a54d7
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 70 deletions.
3 changes: 2 additions & 1 deletion apps/browser/src/autofill/background/overlay.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { COZY_ATTRIBUTES_MAPPING } from "../../../../../libs/cozy/mapping";
import { createOrUpdateCozyDoctype } from "../../../../../libs/cozy/createOrUpdateCozyDoctype";
import { getCozyValue, getAllPapersFromContact } from "../../../../../libs/cozy/getCozyValue";
import _ from "lodash";
import { FILES_DOCTYPE } from "../../../../../libs/cozy/constants";
/* eslint-enable */
/* end Cozy imports */

Expand Down Expand Up @@ -960,7 +961,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
On the ambiguous(phone/address/email) form field:
- Display a menu to select value.
*/
if (focusedFieldModel.doctype === "io.cozy.files") {
if (focusedFieldModel.doctype === FILES_DOCTYPE) {
const availablePapers = (
await getAllPapersFromContact({
client,
Expand Down
27 changes: 12 additions & 15 deletions apps/browser/src/cozy/realtime/RealtimeNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.servic
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";

import { CONTACTS_DOCTYPE, FILES_DOCTYPE } from "../../../../../libs/cozy/constants";
import { convertContactToCipherData } from "../../../../../libs/cozy/contact.helper";
import {
convertNoteToCipherData,
Expand All @@ -26,23 +27,21 @@ export class RealTimeNotifications {
// @ts-expect-error reatime item is not typed as it is dynamically injected at runtime
const realtime = this.client.plugins.realtime;

const doctypeContact = "io.cozy.contacts";
await realtime.subscribe(
"created",
doctypeContact,
CONTACTS_DOCTYPE,
this.dispatchCreateOrUpdateContact.bind(this),
);
await realtime.subscribe(
"updated",
doctypeContact,
CONTACTS_DOCTYPE,
this.dispatchCreateOrUpdateContact.bind(this),
);
await realtime.subscribe("deleted", doctypeContact, this.dispatchDeleteCipher.bind(this));
await realtime.subscribe("deleted", CONTACTS_DOCTYPE, this.dispatchDeleteCipher.bind(this));

const doctypePaper = "io.cozy.files";
// We don't want to listen Creation as it is always followed by an Update notification with more data
await realtime.subscribe("updated", doctypePaper, this.dispatchUpdatePaper.bind(this));
await realtime.subscribe("deleted", doctypePaper, this.dispatchDeleteCipher.bind(this));
await realtime.subscribe("updated", FILES_DOCTYPE, this.dispatchUpdatePaper.bind(this));
await realtime.subscribe("deleted", FILES_DOCTYPE, this.dispatchDeleteCipher.bind(this));

const doctypeThumbnail = "io.cozy.files.thumbnails";
await realtime.subscribe("created", doctypeThumbnail, this.dispatchCreateThumbnail.bind(this));
Expand All @@ -53,14 +52,12 @@ export class RealTimeNotifications {
// @ts-expect-error reatime item is not typed as it is dynamically injected at runtime
const realtime = this.client.plugins.realtime;

const doctypeContact = "io.cozy.contacts";
await realtime.unsubscribe("created", doctypeContact, this.dispatchCreateOrUpdateContact);
await realtime.unsubscribe("updated", doctypeContact, this.dispatchCreateOrUpdateContact);
await realtime.unsubscribe("deleted", doctypeContact, this.dispatchDeleteCipher);
await realtime.unsubscribe("created", CONTACTS_DOCTYPE, this.dispatchCreateOrUpdateContact);
await realtime.unsubscribe("updated", CONTACTS_DOCTYPE, this.dispatchCreateOrUpdateContact);
await realtime.unsubscribe("deleted", CONTACTS_DOCTYPE, this.dispatchDeleteCipher);

const doctypePaper = "io.cozy.files";
await realtime.unsubscribe("updated", doctypePaper, this.dispatchUpdatePaper);
await realtime.unsubscribe("deleted", doctypePaper, this.dispatchDeleteCipher);
await realtime.unsubscribe("updated", FILES_DOCTYPE, this.dispatchUpdatePaper);
await realtime.unsubscribe("deleted", FILES_DOCTYPE, this.dispatchDeleteCipher);

const doctypeThumbnail = "io.cozy.files.thumbnails";
await realtime.unsubscribe("created", doctypeThumbnail, this.dispatchCreateThumbnail);
Expand Down Expand Up @@ -101,7 +98,7 @@ export class RealTimeNotifications {

async upsertPaperFromId(paperId: string) {
const itemFromDb = await fetchPaper(this.client, paperId);
const hydratedData = this.client.hydrateDocuments("io.cozy.files", [itemFromDb])[0];
const hydratedData = this.client.hydrateDocuments(FILES_DOCTYPE, [itemFromDb])[0];

let cipherData;
if (isNote(itemFromDb)) {
Expand Down
5 changes: 3 additions & 2 deletions apps/browser/src/popup/services/cozyClient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { LoginUriView } from "@bitwarden/common/vault/models/view/login-uri.view
import { LoginView } from "@bitwarden/common/vault/models/view/login.view";
import { SecureNoteView } from "@bitwarden/common/vault/models/view/secure-note.view";

import { CONTACTS_DOCTYPE, FILES_DOCTYPE } from "../../../../../libs/cozy/constants";
import { RealTimeNotifications } from "../../cozy/realtime/RealtimeNotifications";
import manifest from "../../manifest.json";

Expand Down Expand Up @@ -151,11 +152,11 @@ export class CozyClientService {
oauthOptions,
schema: {
files: {
doctype: "io.cozy.files",
doctype: FILES_DOCTYPE,
relationships: {
contacts: {
type: HasManyContacts,
doctype: "io.cozy.contacts",
doctype: CONTACTS_DOCTYPE,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.serv
import { PaperType } from "@bitwarden/common/enums/paperType";
import { DomSanitizer } from "@angular/platform-browser";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { FILES_DOCTYPE } from "../../../../../../../libs/cozy/constants";

/* eslint-enable */
/* end Cozy imports */
Expand Down Expand Up @@ -620,7 +621,7 @@ export class ViewComponent extends BaseViewComponent implements OnInit, OnDestro
const client = await this.cozyClientService.getClientInstance();

const printUrl = await client
.collection("io.cozy.files")
.collection(FILES_DOCTYPE)
.getDownloadLinkById(this.cipher.id, this.cipher.name);

window.open(printUrl);
Expand All @@ -632,10 +633,10 @@ export class ViewComponent extends BaseViewComponent implements OnInit, OnDestro
const client = await this.cozyClientService.getClientInstance();

const downloadUrl = await client
.collection("io.cozy.files")
.collection(FILES_DOCTYPE)
.getDownloadLinkById(this.cipher.id, this.cipher.name);

client.collection("io.cozy.files").forceFileDownload(`${downloadUrl}?Dl=1`, this.cipher.name);
client.collection(FILES_DOCTYPE).forceFileDownload(`${downloadUrl}?Dl=1`, this.cipher.name);
}
// Cozy customization end

Expand Down
2 changes: 2 additions & 0 deletions libs/cozy/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CONTACTS_DOCTYPE = "io.cozy.contacts";
export const FILES_DOCTYPE = "io.cozy.files";
11 changes: 6 additions & 5 deletions libs/cozy/createOrUpdateCozyDoctype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";

import { AutofillFieldQualifierType } from "../../apps/browser/src/autofill/enums/autofill-field.enums";

import { CONTACTS_DOCTYPE, FILES_DOCTYPE } from "./constants";
import { getOrCreateAppFolderWithReference } from "./helpers/folder";
import { createPDFWithText } from "./helpers/pdf";
import { COZY_ATTRIBUTES_MAPPING, CozyAttributesModel } from "./mapping";
Expand Down Expand Up @@ -43,11 +44,11 @@ export const createOrUpdateCozyDoctype = async ({
return;
}

const { data: contact } = (await client.query(Q("io.cozy.contacts").getById(cipher.id))) as {
const { data: contact } = (await client.query(Q(CONTACTS_DOCTYPE).getById(cipher.id))) as {
data: IOCozyContact;
};

if (cozyAttributeModel.doctype === "io.cozy.contacts") {
if (cozyAttributeModel.doctype === CONTACTS_DOCTYPE) {
// only update for the moment
const updatedContact = await createOrUpdateCozyContact({
contact,
Expand All @@ -56,7 +57,7 @@ export const createOrUpdateCozyDoctype = async ({
});

await client.save(updatedContact);
} else if (cozyAttributeModel.doctype === "io.cozy.files") {
} else if (cozyAttributeModel.doctype === FILES_DOCTYPE) {
// only create for the moment
const createdPaper = await createOrUpdateCozyPaper({
client,
Expand Down Expand Up @@ -164,10 +165,10 @@ export const createOrUpdateCozyPaper = async ({
);

// Add contact
const fileCollection = client.collection('io.cozy.files')
const fileCollection = client.collection(FILES_DOCTYPE)
const references = [{
_id: contact._id,
_type: 'io.cozy.contacts'
_type: CONTACTS_DOCTYPE
}]

await fileCollection.addReferencedBy(fileCreated, references)
Expand Down
5 changes: 3 additions & 2 deletions libs/cozy/getCozyValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IOCozyFile } from "cozy-client/types/types";

import { CozyAutofillOptions } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";

import { FILES_DOCTYPE } from "./constants";
import { selectPaper, selectDataWithCozyProfile } from "./getCozyValue";

// PROFILES
Expand Down Expand Up @@ -134,7 +135,7 @@ describe("getCozyValue", () => {

const RIB1 = {
_id: "b4698ba56c7d0ae2faeb9571d4e0ce60",
_type: "io.cozy.files",
_type: FILES_DOCTYPE,
name: "RIB 1 - Bob John Doe.pdf",
metadata: {
bicNumber: "BIC99999999",
Expand All @@ -153,7 +154,7 @@ const RIB1 = {

const RIB2 = {
_id: "6bfca732cd8f258cde5b012f3b48dd67",
_type: "io.cozy.files",
_type: FILES_DOCTYPE,
name: "RIB 2 - Bob John Doe.pdf",
metadata: {
bicNumber: "BIC11111111",
Expand Down
13 changes: 7 additions & 6 deletions libs/cozy/getCozyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AutofillField from "../../apps/browser/src/autofill/models/autofill-field
import { CozyAutofillOptions } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";
import { PaperAutoFillConstants } from "../../apps/browser/src/autofill/services/autofill-constants";

import { CONTACTS_DOCTYPE, FILES_DOCTYPE } from "./constants";
import { COZY_ATTRIBUTES_MAPPING, CozyAttributesModel, FILTERS } from "./mapping";

interface GetCozyValueType {
Expand Down Expand Up @@ -36,14 +37,14 @@ export const getCozyValue = async ({
return;
}

if (cozyAttributeModel.doctype === "io.cozy.contacts") {
if (cozyAttributeModel.doctype === CONTACTS_DOCTYPE) {
return await getCozyValueInContact({
client,
contactId,
cozyAttributeModel,
cozyAutofillOptions,
});
} else if (cozyAttributeModel.doctype === "io.cozy.files") {
} else if (cozyAttributeModel.doctype === FILES_DOCTYPE) {
return await getCozyValueInPaper({
client,
contactId,
Expand Down Expand Up @@ -74,7 +75,7 @@ const getCozyValueInContact = async ({
cozyAttributeModel,
cozyAutofillOptions,
}: GetCozyValueInDataType) => {
const { data: contact } = await client.query(Q("io.cozy.contacts").getById(contactId), {
const { data: contact } = await client.query(Q(CONTACTS_DOCTYPE).getById(contactId), {
executeFromStore: true,
});

Expand Down Expand Up @@ -133,7 +134,7 @@ export const selectPaper = ({
cozyAutofillOptions?: CozyAutofillOptions;
}) => {
const papersModels = Object.values(COZY_ATTRIBUTES_MAPPING).filter(
(model) => model.doctype === "io.cozy.files",
(model) => model.doctype === FILES_DOCTYPE,
);

// Example: If we click on a BIC of value "BIC111111", we look in the papers
Expand Down Expand Up @@ -166,7 +167,7 @@ export const getAllPapersFromContact = async ({
cozyAttributeModel: CozyAttributesModel;
}): Promise<IOCozyFile[]> => {
const { data: papers } = await client.query(
Q("io.cozy.files")
Q(FILES_DOCTYPE)
.where({
...cozyAttributeModel.selector,
})
Expand Down Expand Up @@ -246,7 +247,7 @@ export const selectDataWithCozyProfile = (

const isReferencedByContact = (paper: any, contactId: string) => {
return paper?.relationships?.referenced_by?.data?.find(
(reference: any) => reference.id === contactId && reference.type === "io.cozy.contacts",
(reference: any) => reference.id === contactId && reference.type === CONTACTS_DOCTYPE,
);
};

Expand Down
Loading

0 comments on commit 77a54d7

Please sign in to comment.