async function getUserInfo()

in src/createAssessment.ts [85:121]


async function getUserInfo(req: EdgeRequest, accountIdField?: string, usernameField?: string): Promise<UserInfo> {
  const contentType = req.getHeader("content-type");
  let userInfo: UserInfo = { accountId: "", userIds: [] };
  if (contentType && contentType.includes("application/json")) {
    try {
      const body = await req.getBodyJson();
      const accountId = accountIdField ? (body[accountIdField] ?? undefined) : undefined;
      const username = usernameField ? (body[usernameField] ?? undefined) : undefined;

      if (accountId) {
        userInfo = {
          accountId,
          userIds: [{ username: username }],
        };
      }
    } catch (error) {
      console.error("Error parsing json when getting UserInfo:", error);
    }
  } else if (contentType && contentType.includes("application/x-www-form-urlencoded")) {
    try {
      const bodyText = await req.getBodyText();
      const formData = new URLSearchParams(bodyText);
      const accountId = accountIdField ? (formData.get(accountIdField) ?? undefined) : undefined;
      const username = usernameField ? (formData.get(usernameField) ?? undefined) : undefined;

      if (accountId) {
        userInfo = {
          accountId,
          userIds: username ? [{ username }] : [],
        };
      }
    } catch (error) {
      console.error("Error parsing form data when getting UserInfo:", error);
    }
  }
  return userInfo;
}