Skip to content

Commit 9012d10

Browse files
authored
Add option to remove files from identity, contact, company (#590)
1 parent 2371de5 commit 9012d10

File tree

11 files changed

+182
-62
lines changed

11 files changed

+182
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 0.4.3
22

33
* Add endpoints to fetch files as base64 for identity, contacts, companies and bills
4+
* Add option to remove files for identity, contacts and companies - if the file upload id in the payload is missing, it's ignored, if it's explicitly set to undefined, the file is removed
45

56
# 0.4.2
67

clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
too-many-arguments-threshold=13
1+
too-many-arguments-threshold=14

crates/bcr-ebill-api/src/service/company_service.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ pub trait CompanyServiceApi: ServiceTraitBounds {
7878
registration_number: Option<String>,
7979
registration_date: Option<String>,
8080
logo_file_upload_id: Option<String>,
81+
ignore_logo_file_upload_id: bool,
8182
proof_of_registration_file_upload_id: Option<String>,
83+
ignore_proof_of_registration_file_upload_id: bool,
8284
timestamp: u64,
8385
) -> Result<()>;
8486

@@ -406,7 +408,9 @@ impl CompanyServiceApi for CompanyService {
406408
registration_number: Option<String>,
407409
registration_date: Option<String>,
408410
logo_file_upload_id: Option<String>,
411+
ignore_logo_file_upload_id: bool,
409412
proof_of_registration_file_upload_id: Option<String>,
413+
ignore_proof_of_registration_file_upload_id: bool,
410414
timestamp: u64,
411415
) -> Result<()> {
412416
debug!("editing company with id: {id}");
@@ -489,35 +493,53 @@ impl CompanyServiceApi for CompanyService {
489493
changed = true;
490494
}
491495

492-
if !changed
493-
&& logo_file_upload_id.is_none()
496+
// remove the logo
497+
if !ignore_logo_file_upload_id && logo_file_upload_id.is_none() {
498+
company.logo_file = None;
499+
changed = true;
500+
}
501+
502+
// remove the proof of registration
503+
if !ignore_proof_of_registration_file_upload_id
494504
&& proof_of_registration_file_upload_id.is_none()
495505
{
506+
company.proof_of_registration_file = None;
507+
changed = true;
508+
}
509+
510+
if !changed {
496511
return Ok(());
497512
}
498513

499514
let (logo_file, proof_of_registration_file) = match nostr_relays.first() {
500515
Some(nostr_relay) => {
501-
let logo_file = self
502-
.process_upload_file(
516+
let logo_file = if ignore_logo_file_upload_id {
517+
None
518+
} else {
519+
self.process_upload_file(
503520
&logo_file_upload_id,
504521
id,
505522
&full_identity.key_pair.pub_key(),
506523
nostr_relay,
507524
)
508-
.await?;
525+
.await?
526+
};
509527
// only override the picture, if there is a new one
510528
if logo_file.is_some() {
511529
company.logo_file = logo_file.clone();
512530
}
513-
let proof_of_registration_file = self
514-
.process_upload_file(
531+
532+
let proof_of_registration_file = if ignore_proof_of_registration_file_upload_id {
533+
None
534+
} else {
535+
self.process_upload_file(
515536
&proof_of_registration_file_upload_id,
516537
id,
517538
&full_identity.key_pair.pub_key(),
518539
nostr_relay,
519540
)
520-
.await?;
541+
.await?
542+
};
521543
// only override the document, if there is a new one
522544
if proof_of_registration_file.is_some() {
523545
company.proof_of_registration_file = proof_of_registration_file.clone();
@@ -1330,7 +1352,9 @@ pub mod tests {
13301352
None,
13311353
None,
13321354
Some("some_file_id".to_string()),
1355+
false,
13331356
None,
1357+
true,
13341358
1731593928,
13351359
)
13361360
.await;
@@ -1371,7 +1395,9 @@ pub mod tests {
13711395
None,
13721396
None,
13731397
None,
1398+
true,
13741399
None,
1400+
true,
13751401
1731593928,
13761402
)
13771403
.await;
@@ -1424,7 +1450,9 @@ pub mod tests {
14241450
None,
14251451
None,
14261452
None,
1453+
true,
14271454
None,
1455+
true,
14281456
1731593928,
14291457
)
14301458
.await;
@@ -1489,7 +1517,9 @@ pub mod tests {
14891517
None,
14901518
None,
14911519
None,
1520+
true,
14921521
None,
1522+
true,
14931523
1731593928,
14941524
)
14951525
.await;
@@ -2403,7 +2433,9 @@ pub mod tests {
24032433
None,
24042434
None,
24052435
None,
2436+
true,
24062437
None,
2438+
true,
24072439
1731593928
24082440
)
24092441
.await

crates/bcr-ebill-api/src/service/contact_service.rs

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ pub trait ContactServiceApi: ServiceTraitBounds {
6464
city_of_birth_or_registration: Option<String>,
6565
identification_number: Option<String>,
6666
avatar_file_upload_id: Option<String>,
67+
ignore_avatar_file_upload_id: bool,
6768
proof_document_file_upload_id: Option<String>,
69+
ignore_proof_document_file_upload_id: bool,
6870
) -> Result<()>;
6971

7072
/// Adds a new contact
@@ -254,7 +256,9 @@ impl ContactServiceApi for ContactService {
254256
city_of_birth_or_registration: Option<String>,
255257
identification_number: Option<String>,
256258
avatar_file_upload_id: Option<String>,
259+
ignore_avatar_file_upload_id: bool,
257260
proof_document_file_upload_id: Option<String>,
261+
ignore_proof_document_file_upload_id: bool,
258262
) -> Result<()> {
259263
debug!("updating contact with node_id: {node_id}");
260264
validate_node_id_network(node_id)?;
@@ -345,37 +349,51 @@ impl ContactServiceApi for ContactService {
345349
&mut changed,
346350
);
347351

348-
if !changed
349-
&& avatar_file_upload_id.is_none()
350-
&& proof_document_file_upload_id.is_none()
351-
{
352+
// remove the avatar
353+
if !ignore_avatar_file_upload_id && avatar_file_upload_id.is_none() {
354+
contact.avatar_file = None;
355+
changed = true;
356+
}
357+
358+
// remove the proof document
359+
if !ignore_proof_document_file_upload_id && proof_document_file_upload_id.is_none() {
360+
contact.proof_document_file = None;
361+
changed = true;
362+
}
363+
364+
if !changed {
352365
return Ok(());
353366
}
354367

355368
if let Some(nostr_relay) = nostr_relays.first() {
356-
let avatar_file = self
357-
.process_upload_file(
358-
&avatar_file_upload_id,
359-
node_id,
360-
&identity_public_key,
361-
nostr_relay,
362-
)
363-
.await?;
364-
// only override the picture, if there is a new one
365-
if avatar_file.is_some() {
366-
contact.avatar_file = avatar_file;
369+
if !ignore_avatar_file_upload_id {
370+
let avatar_file = self
371+
.process_upload_file(
372+
&avatar_file_upload_id,
373+
node_id,
374+
&identity_public_key,
375+
nostr_relay,
376+
)
377+
.await?;
378+
// only override the picture, if there is a new one
379+
if avatar_file.is_some() {
380+
contact.avatar_file = avatar_file;
381+
}
367382
}
368-
let proof_document_file = self
369-
.process_upload_file(
370-
&proof_document_file_upload_id,
371-
node_id,
372-
&identity_public_key,
373-
nostr_relay,
374-
)
375-
.await?;
376-
// only override the document, if there is a new one
377-
if proof_document_file.is_some() {
378-
contact.proof_document_file = proof_document_file;
383+
384+
if !ignore_proof_document_file_upload_id {
385+
let proof_document_file = self
386+
.process_upload_file(
387+
&proof_document_file_upload_id,
388+
node_id,
389+
&identity_public_key,
390+
nostr_relay,
391+
)
392+
.await?;
393+
// only override the document, if there is a new one
394+
if proof_document_file.is_some() {
395+
contact.proof_document_file = proof_document_file;
396+
}
379397
}
380398
};
381399
}
@@ -812,7 +830,9 @@ pub mod tests {
812830
None,
813831
None,
814832
None,
815-
None
833+
true,
834+
None,
835+
true,
816836
)
817837
.await
818838
.is_err()
@@ -909,7 +929,9 @@ pub mod tests {
909929
None,
910930
None,
911931
None,
932+
true,
912933
None,
934+
true,
913935
)
914936
.await;
915937
assert!(result.is_ok());

crates/bcr-ebill-api/src/service/identity_service.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub trait IdentityServiceApi: ServiceTraitBounds {
3535
city_of_birth: Option<String>,
3636
identification_number: Option<String>,
3737
profile_picture_file_upload_id: Option<String>,
38+
ignore_profile_picture_file_upload_id: bool,
3839
identity_document_file_upload_id: Option<String>,
40+
ignore_identity_document_file_upload_id: bool,
3941
timestamp: u64,
4042
) -> Result<()>;
4143
/// Gets the full local identity, including the key pair and node id
@@ -198,7 +200,9 @@ impl IdentityServiceApi for IdentityService {
198200
city_of_birth: Option<String>,
199201
identification_number: Option<String>,
200202
profile_picture_file_upload_id: Option<String>,
203+
ignore_profile_picture_file_upload_id: bool,
201204
identity_document_file_upload_id: Option<String>,
205+
ignore_identity_document_file_upload_id: bool,
202206
timestamp: u64,
203207
) -> Result<()> {
204208
debug!("updating identity");
@@ -279,37 +283,53 @@ impl IdentityServiceApi for IdentityService {
279283
&mut changed,
280284
);
281285

282-
if !changed
283-
&& profile_picture_file_upload_id.is_none()
286+
// remove the profile picture
287+
if !ignore_profile_picture_file_upload_id && profile_picture_file_upload_id.is_none() {
288+
identity.profile_picture_file = None;
289+
changed = true;
290+
}
291+
292+
// remove the identity document
293+
if !ignore_identity_document_file_upload_id
284294
&& identity_document_file_upload_id.is_none()
285295
{
296+
identity.identity_document_file = None;
297+
changed = true;
298+
}
299+
300+
if !changed {
286301
return Ok(());
287302
}
288303

289304
if let Some(nostr_relay) = nostr_relays.first() {
290-
profile_picture_file = self
291-
.process_upload_file(
292-
&profile_picture_file_upload_id,
293-
&identity.node_id,
294-
&keys.pub_key(),
295-
nostr_relay,
296-
)
297-
.await?;
298-
// only override the picture, if there is a new one
299-
if profile_picture_file.is_some() {
300-
identity.profile_picture_file = profile_picture_file.clone();
305+
if !ignore_profile_picture_file_upload_id {
306+
profile_picture_file = self
307+
.process_upload_file(
308+
&profile_picture_file_upload_id,
309+
&identity.node_id,
310+
&keys.pub_key(),
311+
nostr_relay,
312+
)
313+
.await?;
314+
// only override the picture, if there is a new one
315+
if profile_picture_file.is_some() {
316+
identity.profile_picture_file = profile_picture_file.clone();
317+
}
301318
}
302-
identity_document_file = self
303-
.process_upload_file(
304-
&identity_document_file_upload_id,
305-
&identity.node_id,
306-
&keys.pub_key(),
307-
nostr_relay,
308-
)
309-
.await?;
310-
// only override the document, if there is a new one
311-
if identity_document_file.is_some() {
312-
identity.identity_document_file = identity_document_file.clone();
319+
320+
if !ignore_identity_document_file_upload_id {
321+
identity_document_file = self
322+
.process_upload_file(
323+
&identity_document_file_upload_id,
324+
&identity.node_id,
325+
&keys.pub_key(),
326+
nostr_relay,
327+
)
328+
.await?;
329+
// only override the document, if there is a new one
330+
if identity_document_file.is_some() {
331+
identity.identity_document_file = identity_document_file.clone();
332+
}
313333
}
314334
};
315335
}
@@ -939,7 +959,9 @@ mod tests {
939959
None,
940960
None,
941961
None,
962+
true,
942963
None,
964+
true,
943965
1731593928,
944966
)
945967
.await;
@@ -971,7 +993,9 @@ mod tests {
971993
None,
972994
None,
973995
None,
996+
true,
974997
None,
998+
true,
975999
1731593928,
9761000
)
9771001
.await;
@@ -1022,7 +1046,9 @@ mod tests {
10221046
None,
10231047
None,
10241048
None,
1049+
true,
10251050
None,
1051+
true,
10261052
1731593928,
10271053
)
10281054
.await;

crates/bcr-ebill-wasm/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ <h2>Contact Testing</h2>
5050
<button type="button" id="contact_test">Create Contact for node id</button>
5151
<button type="button" id="contact_test_anon">Create Anon Contact for node id</button>
5252
<button type="button" id="fetch_contacts">Fetch Contacts</button>
53+
<button type="button" id="remove_contact_avatar">Remove Avatar</button>
5354
<button type="button" id="delete_contact">Delete Contact</button>
5455
</div>
5556
</div>

0 commit comments

Comments
 (0)