Skip to content

Commit

Permalink
[sc-11293] Display message when 402 (#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
operramon authored Dec 16, 2024
1 parent 710facf commit 86e5ff6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
23 changes: 23 additions & 0 deletions libs/common/common.babel
Original file line number Diff line number Diff line change
Expand Up @@ -38325,6 +38325,29 @@
<folder_node>
<name>toast</name>
<children>
<concept_node>
<name>blocked</name>
<description/>
<comment/>
<translations>
<translation>
<language>ca-ES</language>
<approved>false</approved>
</translation>
<translation>
<language>en-US</language>
<approved>false</approved>
</translation>
<translation>
<language>es-ES</language>
<approved>false</approved>
</translation>
<translation>
<language>fr-FR</language>
<approved>false</approved>
</translation>
</translations>
</concept_node>
<concept_node>
<name>failed</name>
<description/>
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/assets/i18n/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@
"upload.select_file": "Arrossegueu els fitxers o feu clic aquí",
"upload.takes_time": "Nuclia necessita una estona per a processar els fitxers.",
"upload.texts": "Afegir recursos de text",
"upload.toast.blocked": "S'ha esgotat la quota de pujada, necessites actualitzar el teu compte.",
"upload.toast.failed": "Errors durant la càrrega",
"upload.toast.limit": "El teu compte ha enviat massa peticions durant un període curt de temps. Torna-ho a provar d’aquí uns instants.",
"upload.toast.successful": "Pujada correctament",
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@
"upload.select_file": "Drop your file(s) or click here",
"upload.takes_time": "We need some time to process the files. Feel free to close this dialog box and keep working.",
"upload.texts": "Add text resources",
"upload.toast.blocked": "Your upload quota has been reached, you need to upgrade your account.",
"upload.toast.failed": "Errors during upload",
"upload.toast.limit": "Your account has been issuing too many requests over a short period. Try again in a moment.",
"upload.toast.successful": "Upload successful",
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/assets/i18n/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@
"upload.select_file": "Arrastre sus archivos o haga clic aquí",
"upload.takes_time": "Nuclia necesita un rato para procesar los archivos.",
"upload.texts": "Añadir recursos de texto",
"upload.toast.blocked": "Se ha alcanzado la cuota de subida, necesita actualizar tu cuenta.",
"upload.toast.failed": "Errores durante la carga",
"upload.toast.limit": "Tu cuenta ha estado enviando demasiadas peticiones en un período corto de tiempo. Inténtalo de nuevo dentro de unos instantes.",
"upload.toast.successful": "Subida exitosa",
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/assets/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@
"upload.select_file": "Déposez votre (vos) fichier(s) ou cliquez ici",
"upload.takes_time": "Nous avons besoin d’un certain temps pour traiter les fichiers. N’hésitez pas à fermer cette boîte de dialogue et à poursuivre votre travail.",
"upload.texts": "Ajouter des ressources textuelles",
"upload.toast.blocked": "Votre quota de téléchargement a été atteint, vous devez mettre à niveau votre compte.",
"upload.toast.failed": "Erreurs lors du téléchargement",
"upload.toast.limit": "Votre compte a envoyé trop de requêtes en peu de temps. Merci de réessayer dans quelques instants.",
"upload.toast.successful": "Téléchargement réussi",
Expand Down
15 changes: 11 additions & 4 deletions libs/common/src/lib/upload/upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ export class UploadService {
this.uploadFiles(files, (progress) => {
if (progress.completed) {
if (progress.failed === 0 || progress.failed === progress.conflicts) {
this.onUploadComplete(true, false, (progress.conflicts || 0) < progress.files.length);
this.onUploadComplete(true, false, false, (progress.conflicts || 0) < progress.files.length);
} else if (!hasNotifiedError) {
hasNotifiedError = true;
this.onUploadComplete(false, (progress.limitExceeded || 0) > 0);
this.onUploadComplete(false, (progress.limitExceeded || 0) > 0, (progress.blocked || 0) > 0);
}
}
}),
Expand Down Expand Up @@ -343,13 +343,17 @@ export class UploadService {
bulkUpload(uploads: Observable<any>[]): Observable<{ errors: number }> {
let errors = 0;
let errors429 = 0;
let blocked = false;
uploads = uploads.map((upload) =>
upload.pipe(
catchError((error) => {
errors += 1;
if (error?.status === 429) {
errors429 += 1;
}
if (error?.status === 402) {
blocked = true;
}
return of(null);
}),
),
Expand All @@ -358,7 +362,7 @@ export class UploadService {
mergeMap((obs) => obs, 6),
toArray(),
tap(() => {
this.onUploadComplete(errors === 0, errors429 > 0);
this.onUploadComplete(errors === 0, errors429 > 0, blocked);
}),
map(() => ({ errors })),
);
Expand Down Expand Up @@ -396,10 +400,13 @@ export class UploadService {
);
}

onUploadComplete(success: boolean, limitExceeded = false, showNotification = true) {
onUploadComplete(success: boolean, limitExceeded = false, blocked = false, showNotification = true) {
if (showNotification) {
success ? this.toaster.success('upload.toast.successful') : this.toaster.warning('upload.toast.failed');
}
if (blocked) {
this.toaster.error('upload.toast.blocked');
}
if (limitExceeded) {
this.toaster.error('upload.toast.limit');
this.tracking.logEvent('upload_limit_exceeded');
Expand Down
20 changes: 16 additions & 4 deletions libs/sdk-core/src/lib/db/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface UploadResponse {
completed?: boolean;
conflict?: boolean;
limitExceeded?: boolean;
blocked?: boolean;
}

export interface UploadStatus {
Expand All @@ -39,6 +40,7 @@ export interface UploadStatus {
failed: number;
conflicts?: number;
limitExceeded?: number;
blocked?: number;
}

export interface FileUploadStatus {
Expand All @@ -48,6 +50,7 @@ export interface FileUploadStatus {
failed: boolean;
conflicts?: boolean;
limitExceeded?: boolean;
blocked?: boolean;
}

export interface FileWithMetadata extends File {
Expand Down Expand Up @@ -143,7 +146,7 @@ export const uploadFile = (
return of({ failed: true });
}
}),
catchError((error) => of({ failed: true, limitExceeded: error.status === 429 })),
catchError((error) => of({ failed: true, limitExceeded: error.status === 429, blocked: error.status === 402 })),
);
};

Expand Down Expand Up @@ -188,7 +191,12 @@ export const TUSuploadFile = (
merge(
of(res).pipe(
filter((res) => res.status !== 201 || !res.headers.get('location')),
map((res) => ({ failed: true, conflict: res.status === 409, limitExceeded: res.status === 429 })),
map((res) => ({
failed: true,
conflict: res.status === 409,
limitExceeded: res.status === 429,
blocked: res.status === 402,
})),
),
of(res).pipe(
filter((res) => res.status === 201 && !!res.headers.get('location')),
Expand Down Expand Up @@ -229,7 +237,7 @@ export const TUSuploadFile = (
}),
catchError(() => {
failed = true;
return of({ failed: true, limitExceeded: res.status === 429 });
return of({ failed: true, limitExceeded: res.status === 429, blocked: res.status === 402 });
}),
);
}),
Expand Down Expand Up @@ -289,6 +297,9 @@ export const batchUpload = (
if (res.status.limitExceeded) {
fileStatus.limitExceeded = true;
}
if (res.status.blocked) {
fileStatus.blocked = true;
}
if (res.status.completed) {
fileStatus.uploaded = true;
}
Expand All @@ -300,12 +311,13 @@ export const batchUpload = (
const failed = filesStatus.filter((item) => item.failed).length;
const conflicts = filesStatus.filter((item) => item.conflicts).length;
const limitExceeded = filesStatus.filter((item) => item.limitExceeded).length;
const blocked = filesStatus.filter((item) => item.blocked).length;
const uploaded = filesStatus.filter((item) => item.uploaded).length;
const completed = filesStatus.filter((item) => !item.failed && !item.uploaded).length === 0;
const progress = Math.round(
(filesStatus.reduce((acc, status) => acc + (status.file.size * status.progress) / 100, 0) / totalSize) * 100,
);
return { files: filesStatus, progress, completed, uploaded, failed, conflicts, limitExceeded };
return { files: filesStatus, progress, completed, uploaded, failed, conflicts, limitExceeded, blocked };
}),
);
};
Expand Down

0 comments on commit 86e5ff6

Please sign in to comment.