1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 05:30:01 +00:00

fixing how the actions are triggered and accessing the metric from the config

This commit is contained in:
Tom
2025-03-18 15:20:38 -04:00
parent 7699752390
commit 38fc47ff2a
2 changed files with 40 additions and 11 deletions

View File

@@ -140,7 +140,8 @@ export class ItemCreatedCountConfig implements Achievement {
name: name,
validator: Type.Threshold,
active: {
metric: `item-${cipherType ? `${CipherType[cipherType]}-` : ""}quantity` as MetricId,
metric:
`item-${cipherType ? `${CipherType[cipherType].toLowerCase()}-` : ""}quantity` as MetricId,
low: threshold - 1,
high: threshold,
},

View File

@@ -1,7 +1,11 @@
import { CipherType } from "../../../vault/enums";
import { EventFormat } from "../../log/ecs-format";
import { earnedEvent, progressEvent } from "../achievement-events";
import { AchievementProgressEvent, AchievementValidator, MetricId } from "../types";
import {
AchievementProgressEvent,
AchievementValidator,
MetricId,
UserActionEvent,
} from "../types";
import { ItemCreatedCountConfig } from "./config/item-created-count-config";
@@ -15,8 +19,15 @@ import { ItemCreatedCountConfig } from "./config/item-created-count-config";
export class VaultItemCreatedCountValidator implements AchievementValidator {
private itemCreatedProgress: MetricId;
constructor(private config: ItemCreatedCountConfig) {
this.itemCreatedProgress =
`item-${config.cipherType ? `${CipherType[config.cipherType]}-` : ""}quantity` as MetricId;
// All of the configs for created items must have a metric.
// This checks the types to allow us to assign the metric.
if (config.active === "until-earned") {
throw new Error(
`${config.achievement}: invalid configuration; 'active' must contain a metric`,
);
}
this.itemCreatedProgress = config.active.metric;
}
base: AchievementValidator;
@@ -36,14 +47,11 @@ export class VaultItemCreatedCountValidator implements AchievementValidator {
return this.config.hidden;
}
trigger(item: EventFormat) {
return (
item.action ===
`vault-${this.config.cipherType ? `${CipherType[this.config.cipherType]}-` : ""}item-added`
);
trigger(item: UserActionEvent) {
return item.action === "vault-item-added" && this.validateItemType(item);
}
measure(_item: EventFormat, progress: Map<MetricId, number>) {
measure(_item: UserActionEvent, progress: Map<MetricId, number>) {
const value = 1 + (progress.get(this.itemCreatedProgress) ?? 0);
return [progressEvent(this.itemCreatedProgress, value)];
}
@@ -52,4 +60,24 @@ export class VaultItemCreatedCountValidator implements AchievementValidator {
const value = progress.get(this.itemCreatedProgress) ?? 0;
return value >= this.config.threshold ? [earnedEvent(this.achievement)] : [];
}
/**
* Will check the vault item types. The UserAction is lower case and the
* cipher type is capitalized/camel case. Making it all lowercase will make
* an even comparison. We are coupled to the CipherType enum unless any better
* suggestions.
* @param item The event data checking the trigger
* @returns true or false if the cipherType matches
*/
private validateItemType(item: UserActionEvent): boolean {
// If the config's cipher type is not present no need to check
// for the type.
if (!this.config.cipherType) {
return true;
}
const lowerConfigType = CipherType[this.config.cipherType].toLowerCase();
const lowerItemType = item.labels?.["vault-item-type"].toString().toLowerCase();
return lowerItemType === lowerConfigType;
}
}