Skip to content
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

Expose saving a field in a contact or a paper for inline menu #249

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions apps/browser/src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,12 @@
"sharedWithKonnector": {
"message": "Shared with Cozy Connector"
},
"administrativeFolder": {
"message": "Administrative"
},
"papersFolder": {
"message": "My papers"
},
"movedItemToFolder": {
"message": "Element moved"
},
Expand Down
6 changes: 6 additions & 0 deletions apps/browser/src/_locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,12 @@
"sharedWithKonnector": {
"message": "Partagé avec un Connecteur Cozy"
},
"administrativeFolder": {
"message": "Administratif"
},
"papersFolder": {
"message": "Mes papiers"
},
"movedItemToFolder": {
"message": "Élément déplacé"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,13 @@ export class OverlayBackground implements OverlayBackgroundInterface {
const client = await this.cozyClientService.getClientInstance();
const cipher = this.inlineMenuCiphers.get(inlineMenuCipherId);

await createOrUpdateCozyDoctype({ client, cipher, fieldQualifier, newAutofillValue });
await createOrUpdateCozyDoctype({
client,
cipher,
fieldQualifier,
newAutofillValue,
i18nService: this.i18nService,
});
}
}

Expand Down
81 changes: 80 additions & 1 deletion libs/cozy/createOrUpdateCozyDoctype.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import CozyClient, { Q } from "cozy-client";
import CozyClient, { Q, models } from "cozy-client";
import { IOCozyContact } from "cozy-client/types/types";
import * as _ from "lodash";

import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";

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

import { getOrCreateAppFolderWithReference } from "./helpers/folder";
import { createPDFWithText } from "./helpers/pdf";
import { COZY_ATTRIBUTES_MAPPING, CozyAttributesModel } from "./mapping";

const {
document: { Qualification, locales },
file: { uploadFileWithConflictStrategy },
} = models;

interface AutofillValue {
value: string;
type?: string;
Expand All @@ -19,13 +27,15 @@ interface CreateOrUpdateCozyDoctypeType {
cipher: CipherView;
fieldQualifier: AutofillFieldQualifierType;
newAutofillValue: AutofillValue;
i18nService: I18nService;
}

export const createOrUpdateCozyDoctype = async ({
client,
cipher,
fieldQualifier,
newAutofillValue,
i18nService,
}: CreateOrUpdateCozyDoctypeType) => {
const cozyAttributeModel = COZY_ATTRIBUTES_MAPPING[fieldQualifier];

Expand All @@ -46,6 +56,16 @@ export const createOrUpdateCozyDoctype = async ({
});

await client.save(updatedContact);
} else if (cozyAttributeModel.doctype === "io.cozy.files") {
// only create for the moment
const createdPaper = await createOrUpdateCozyPaper({
client,
cozyAttributeModel,
newAutofillValue,
i18nService,
});

await client.save(createdPaper);
}
};

Expand Down Expand Up @@ -92,3 +112,62 @@ export const createOrUpdateCozyContact = async ({

return contact;
};

interface CreateOrUpdateCozyPaperType {
client: CozyClient;
cozyAttributeModel: CozyAttributesModel;
newAutofillValue: AutofillValue;
i18nService: I18nService;
}

export const createOrUpdateCozyPaper = async ({
client,
cozyAttributeModel,
newAutofillValue,
i18nService,
}: CreateOrUpdateCozyPaperType): Promise<any> => {
const [, qualificationLabelValue] = Object.entries(cozyAttributeModel.selector)[0];

const qualification = Qualification.getByLabel(qualificationLabelValue as string);

// Create the PDF
const pdfText = `${qualificationLabelValue} ${newAutofillValue.value}`;
const pdfBytes = await createPDFWithText(pdfText);

// Build the io.cozy.files document
const dir = await getOrCreateAppFolderWithReference(client, i18nService);

const t = locales.getBoundT(i18nService.translationLocale || "en");

const paperOptions = {
name: t(`Scan.items.${qualification.label}`) + ".pdf",
contentType: "application/pdf",
metadata: {
qualification,
paperProps: {
isBlank: true,
},
},
dirId: dir._id,
conflictStrategy: "rename",
};

_.set(paperOptions, cozyAttributeModel.path, newAutofillValue.value);

const { data: fileCreated } = await uploadFileWithConflictStrategy(
client,
pdfBytes,
paperOptions,
);

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

// await fileCollection.addReferencedBy(fileCreated, references)

Comment on lines +163 to +170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je suppose pour la suite ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactement, il y a encore quelques questionnements sur cette partie, mais "le reste marche"

return fileCreated;
};
36 changes: 36 additions & 0 deletions libs/cozy/helpers/folder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import CozyClient, { models } from "cozy-client";

import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";

const { MAGIC_FOLDERS, ensureMagicFolder, getReferencedFolder } = models.folder;

export const APPS_DOCTYPE = "io.cozy.apps";
const APP_DIR_REF = `${APPS_DOCTYPE}/mypapers`;

export const getOrCreateAppFolderWithReference = async (
client: CozyClient,
i18nService: I18nService,
) => {
const existingFolders = await getReferencedFolder(client, {
_id: APP_DIR_REF,
_type: APPS_DOCTYPE,
});

if (existingFolders) {
return existingFolders;
} else {
const { path: administrativeFolderPath } = await ensureMagicFolder(
client,
MAGIC_FOLDERS.ADMINISTRATIVE,
`/${i18nService.t("administrativeFolder")}`,
);

const appFolder = await ensureMagicFolder(
client,
MAGIC_FOLDERS.PAPERS,
`${administrativeFolderPath}/${i18nService.t("papersFolder")}`,
);

return appFolder;
}
};
24 changes: 24 additions & 0 deletions libs/cozy/helpers/pdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PDFDocument, StandardFonts, rgb } from "pdf-lib";

export const createPDFWithText = async (text: string): Promise<Uint8Array> => {
const pdfDoc = await PDFDocument.create();

const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman);

const page = pdfDoc.addPage();

const { height } = page.getSize();

const fontSize = 30;
page.drawText(text, {
x: 50,
y: height - 4 * fontSize,
size: fontSize,
font: timesRomanFont,
color: rgb(0, 0.53, 0.71),
});

const pdfBytes = await pdfDoc.save();

return pdfBytes;
};
Loading