mongodb#UpdateResult TypeScript Examples

The following examples show how to use mongodb#UpdateResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: config.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function saveConfig(
  uid: string,
  config: object
): Promise<UpdateResult> {
  const configChanges = _.mapKeys(config, (_value, key) => `config.${key}`);
  return await db
    .collection<any>("configs")
    .updateOne({ uid }, { $set: configChanges }, { upsert: true });
}
Example #2
Source File: result.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function updateTags(
  uid: string,
  resultId: string,
  tags: string[]
): Promise<UpdateResult> {
  const result = await db
    .collection<MonkeyTypesResult>("results")
    .findOne({ _id: new ObjectId(resultId), uid });
  if (!result) throw new MonkeyError(404, "Result not found");
  const userTags = await getTags(uid);
  const userTagIds = userTags.map((tag) => tag._id.toString());
  let validTags = true;
  tags.forEach((tagId) => {
    if (!userTagIds.includes(tagId)) validTags = false;
  });
  if (!validTags) {
    throw new MonkeyError(422, "One of the tag id's is not valid");
  }
  return await db
    .collection<MonkeyTypesResult>("results")
    .updateOne({ _id: new ObjectId(resultId), uid }, { $set: { tags } });
}
Example #3
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function updateName(
  uid: string,
  name: string
): Promise<UpdateResult> {
  if (!isUsernameValid(name)) {
    throw new MonkeyError(400, "Invalid username");
  }
  if (!(await isNameAvailable(name))) {
    throw new MonkeyError(409, "Username already taken", name);
  }

  const user = await getUser(uid, "update name");

  const oldName = user.name;

  if (
    !user?.needsToChangeName &&
    Date.now() - (user.lastNameChange ?? 0) < THIRTY_DAYS_IN_SECONDS
  ) {
    throw new MonkeyError(409, "You can change your name once every 30 days");
  }

  await getUsersCollection().updateOne(
    { uid },
    {
      $push: { nameHistory: oldName },
    }
  );
  return await getUsersCollection().updateOne(
    { uid },
    {
      $set: { name, lastNameChange: Date.now() },
      $unset: { needsToChangeName: "" },
    }
  );
}
Example #4
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function clearPb(uid: string): Promise<UpdateResult> {
  return await getUsersCollection().updateOne(
    { uid },
    {
      $set: {
        personalBests: {
          custom: {},
          quote: {},
          time: {},
          words: {},
          zen: {},
        },
        lbPersonalBests: {
          time: {},
        },
      },
    }
  );
}
Example #5
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function editTag(
  uid: string,
  _id: string,
  name: string
): Promise<UpdateResult> {
  const user = await getUser(uid, "edit tag");
  if (
    user.tags === undefined ||
    user.tags.filter((t) => t._id.toHexString() === _id).length === 0
  ) {
    throw new MonkeyError(404, "Tag not found");
  }
  return await getUsersCollection().updateOne(
    {
      uid: uid,
      "tags._id": new ObjectId(_id),
    },
    { $set: { "tags.$.name": name } }
  );
}
Example #6
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function removeTag(
  uid: string,
  _id: string
): Promise<UpdateResult> {
  const user = await getUser(uid, "remove tag");
  if (
    user.tags === undefined ||
    user.tags.filter((t) => t._id.toHexString() == _id).length === 0
  ) {
    throw new MonkeyError(404, "Tag not found");
  }
  return await getUsersCollection().updateOne(
    {
      uid: uid,
      "tags._id": new ObjectId(_id),
    },
    { $pull: { tags: { _id: new ObjectId(_id) } } }
  );
}
Example #7
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function removeTagPb(
  uid: string,
  _id: string
): Promise<UpdateResult> {
  const user = await getUser(uid, "remove tag pb");
  if (
    user.tags === undefined ||
    user.tags.filter((t) => t._id.toHexString() == _id).length === 0
  ) {
    throw new MonkeyError(404, "Tag not found");
  }
  return await getUsersCollection().updateOne(
    {
      uid: uid,
      "tags._id": new ObjectId(_id),
    },
    { $set: { "tags.$.personalBests": {} } }
  );
}
Example #8
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function updateLbMemory(
  uid: string,
  mode: MonkeyTypes.Mode,
  mode2: MonkeyTypes.Mode2<MonkeyTypes.Mode>,
  language: string,
  rank: number
): Promise<UpdateResult> {
  const user = await getUser(uid, "update lb memory");
  if (user.lbMemory === undefined) user.lbMemory = {};
  if (user.lbMemory[mode] === undefined) user.lbMemory[mode] = {};
  if (user.lbMemory[mode][mode2] === undefined) {
    user.lbMemory[mode][mode2] = {};
  }
  user.lbMemory[mode][mode2][language] = rank;
  return await getUsersCollection().updateOne(
    { uid },
    {
      $set: { lbMemory: user.lbMemory },
    }
  );
}
Example #9
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function resetPb(uid: string): Promise<UpdateResult> {
  await getUser(uid, "reset pb");
  return await getUsersCollection().updateOne(
    { uid },
    {
      $set: {
        personalBests: {
          time: {},
          custom: {},
          quote: {},
          words: {},
          zen: {},
        },
      },
    }
  );
}
Example #10
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function updateTypingStats(
  uid: string,
  restartCount: number,
  timeTyping: number
): Promise<UpdateResult> {
  return await getUsersCollection().updateOne(
    { uid },
    {
      $inc: {
        startedTests: restartCount + 1,
        completedTests: 1,
        timeTyping,
      },
    }
  );
}
Example #11
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function incrementBananas(
  uid: string,
  wpm
): Promise<UpdateResult | null> {
  const user = await getUser(uid, "increment bananas");

  let best60: number | undefined;
  const personalBests60 = user.personalBests?.time[60];

  if (personalBests60) {
    best60 = Math.max(...personalBests60.map((best) => best.wpm));
  }

  if (best60 === undefined || wpm >= best60 - best60 * 0.25) {
    //increment when no record found or wpm is within 25% of the record
    return await getUsersCollection().updateOne(
      { uid },
      { $inc: { bananas: 1 } }
    );
  }

  return null;
}
Example #12
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function removeTheme(uid: string, _id): Promise<UpdateResult> {
  const user = await getUser(uid, "remove theme");

  if (themeDoesNotExist(user.customThemes, _id)) {
    throw new MonkeyError(404, "Custom theme not found");
  }

  return await getUsersCollection().updateOne(
    {
      uid: uid,
      "customThemes._id": new ObjectId(_id),
    },
    { $pull: { customThemes: { _id: new ObjectId(_id) } } }
  );
}
Example #13
Source File: user.ts    From monkeytype with GNU General Public License v3.0 6 votes vote down vote up
export async function editTheme(
  uid: string,
  _id,
  theme
): Promise<UpdateResult> {
  const user = await getUser(uid, "edit theme");

  if (themeDoesNotExist(user.customThemes, _id)) {
    throw new MonkeyError(404, "Custom Theme not found");
  }

  return await getUsersCollection().updateOne(
    {
      uid: uid,
      "customThemes._id": new ObjectId(_id),
    },
    {
      $set: {
        "customThemes.$.name": theme.name,
        "customThemes.$.colors": theme.colors,
      },
    }
  );
}
Example #14
Source File: user.ts    From monkeytype with GNU General Public License v3.0 5 votes vote down vote up
export async function unlinkDiscord(uid: string): Promise<UpdateResult> {
  await getUser(uid, "unlink discord");

  return await getUsersCollection().updateOne(
    { uid },
    { $unset: { discordId: "", discordAvatar: "" } }
  );
}