setTimezone.ts 861 B

12345678910111213141516171819202122
  1. import { eventHandler, readBody } from 'h3';
  2. import { verifyAccessToken } from '~/utils/jwt-utils';
  3. import { TIME_ZONE_OPTIONS } from '~/utils/mock-data';
  4. import { unAuthorizedResponse, useResponseSuccess } from '~/utils/response';
  5. import { setTimezone } from '~/utils/timezone-utils';
  6. export default eventHandler(async (event) => {
  7. const userinfo = verifyAccessToken(event);
  8. if (!userinfo) {
  9. return unAuthorizedResponse(event);
  10. }
  11. const body = await readBody<{ timezone?: unknown }>(event);
  12. const timezone =
  13. typeof body?.timezone === 'string' ? body.timezone : undefined;
  14. const allowed = TIME_ZONE_OPTIONS.some((o) => o.timezone === timezone);
  15. if (!timezone || !allowed) {
  16. setResponseStatus(event, 400);
  17. return useResponseError('Bad Request', 'Invalid timezone');
  18. }
  19. setTimezone(timezone);
  20. return useResponseSuccess({});
  21. });