Repository: strapi/strapi
File: packages/core/admin/server/src/services/homepage.ts (TypeScript)

Code before:
      apiTokens: countApiTokens,
    };
  };

  const getUserLayout = async (
    userId: number
  ): Promise<UserLayout | null> => {
    const store = await strapi.store(STORE);
    const key = keyFor(userId);

    const value = (await store.get({ key })) as unknown;
    if (!value) {
      // nothing saved yet
      return null;
    }

    return UserLayoutSchema.parse(value);
  };

  const updateUserLayout = async (
    userId: number,
    input: unknown
  ): Promise<UserLayout> => {
    const write: UserLayoutWrite = UserLayoutWriteSchema.parse(input);

    const store = await strapi.store(STORE);
    const key = keyFor(userId);

    const currentRaw = (await store.get({ key })) as unknown;
    const current: UserLayout | null = currentRaw
      ? UserLayoutSchema.parse(currentRaw)
      : null;

    // Final order: replace only if provided
    const orderNext = write.order ?? current?.order ?? [];

    // Build widths ONLY for UIDs present in the final order (prunes removed)
    const widthsNext = {} as any;
    for (const uid of orderNext) {
      const incoming = write.widths?.[uid];
      const existing = current?.widths[uid];
      widthsNext[uid] = (incoming ?? existing ?? DEFAULT_WIDTH);
    }

    const next: UserLayout = {
      version: (current?.version ?? 0) + 1,
      order: orderNext,
      widths: widthsNext,
      updatedAt: new Date().toISOString(),
    };


Review comment (suggestion):
No need to await here, strapi.store doesn't return a promise, only the methods of the store do.

Also, you can reuse the same store object for both of your methods if you define it outside of them. In that case you likely would not need the STORE constant anymore, you could inline it directly

Code after:
      contentTypes: contentTypes.length,
      components: components.length,
      locales: countLocales,
      admins: countAdmins,
      webhooks: countWebhooks,
      apiTokens: countApiTokens,
    };
  };

  const getHomepageLayout = async (userId: number): Promise<HomepageLayout | null> => {
    const key = keyFor(userId);
    const value = await adminStore.get({ key });
    if (!value) {
      // nothing saved yet
      return null;
    }

    return HomepageLayoutSchema.parse(value);
  };

  const updateHomepageLayout = async (userId: number, input: unknown): Promise<HomepageLayout> => {
    const write: HomepageLayoutWrite = HomepageLayoutWriteSchema.parse(input);

    const key = keyFor(userId);

    const currentRaw = await adminStore.get({ key });
    const current: HomepageLayout | null = currentRaw
      ? HomepageLayoutSchema.parse(currentRaw)
      : null;

    const widgetsNext = write.widgets ?? current?.widgets ?? [];

    // Normalize widths (fill defaults where missing)
    const normalizedWidgets = widgetsNext.map((w) => {
      const prev = current?.widgets.find((cw) => cw.uid === w.uid);
      return {
        uid: w.uid,
        width: w.width ?? prev?.width ?? DEFAULT_WIDTH,
      };
    });

    const next: HomepageLayout = {
      version: write.version ?? 1,
      widgets: normalizedWidgets,
      updatedAt: write.updatedAt ?? new Date().toISOString(),
    };

    await adminStore.set({ key, value: next });
    return next;
  };
  return {