Yuki Takei пре 1 година
родитељ
комит
e6c6b08756

+ 22 - 13
packages/core/src/models/serializers/attachment-serializer.ts

@@ -1,24 +1,33 @@
 import type { IAttachment } from '~/interfaces';
 import type { IAttachment } from '~/interfaces';
 
 
-import { isPopulated, type Ref } from '../../interfaces/common';
+import { isPopulated, isRef, type Ref } from '../../interfaces/common';
 
 
 import { serializeUserSecurely, type IUserSerializedSecurely } from './user-serializer';
 import { serializeUserSecurely, type IUserSerializedSecurely } from './user-serializer';
 
 
-export type IAttachmentSerializedSecurely = Omit<IAttachment, 'creator'> & { creator?: Ref<IUserSerializedSecurely> };
+export type IAttachmentSerializedSecurely<A extends IAttachment = IAttachment> = Omit<A, 'creator'> & { creator?: Ref<IUserSerializedSecurely> };
 
 
-export const serializeAttachmentSecurely = (attachment?: Ref<IAttachment>): Ref<IAttachmentSerializedSecurely> | undefined => {
-  // return when it is not a user object
-  if (attachment == null || !isPopulated(attachment)) {
-    return attachment;
-  }
+const omitInsecureAttributes = <A extends IAttachment>(attachment: A): IAttachmentSerializedSecurely<A> => {
+  const { creator, ...rest } = attachment;
 
 
-  // serialize User data
-  const { _id, creator, ...restAttachmentProperties } = attachment;
+  const secureCreator = creator == null
+    ? undefined
+    : serializeUserSecurely(creator);
 
 
   return {
   return {
-    _id,
-    creator: serializeUserSecurely(creator),
-    ...restAttachmentProperties,
+    creator: secureCreator,
+    ...rest,
   };
   };
-
 };
 };
+
+
+export function serializeAttachmentSecurely<A extends IAttachment>(attachment?: A): IAttachmentSerializedSecurely<A>;
+export function serializeAttachmentSecurely<A extends IAttachment>(attachment?: Ref<A>): Ref<IAttachmentSerializedSecurely<A>>;
+export function serializeAttachmentSecurely<A extends IAttachment>(attachment?: A | Ref<A>)
+    : undefined | IAttachmentSerializedSecurely<A> | Ref<IAttachmentSerializedSecurely<A>> {
+
+  if (attachment == null) return attachment;
+
+  if (isRef(attachment) && !isPopulated(attachment)) return attachment;
+
+  return omitInsecureAttributes(attachment);
+}

+ 18 - 18
packages/core/src/models/serializers/user-serializer.ts

@@ -1,30 +1,30 @@
-import { isPopulated, type Ref } from '../../interfaces/common';
+import { isPopulated, isRef, type Ref } from '../../interfaces/common';
 import type { IUser } from '../../interfaces/user';
 import type { IUser } from '../../interfaces/user';
 
 
-export type IUserSerializedSecurely = Omit<IUser, 'password' | 'apiToken' | 'email'> & { email?: string };
+export type IUserSerializedSecurely<U extends IUser = IUser> = Omit<U, 'password' | 'apiToken' | 'email'> & { email?: string };
 
 
-export const omitInsecureAttributes = (user: IUser): IUserSerializedSecurely => {
-  // eslint-disable-next-line @typescript-eslint/no-unused-vars
-  const { password, apiToken, ...rest } = user;
+export const omitInsecureAttributes = <U extends IUser = IUser>(user: U): IUserSerializedSecurely<U> => {
+  const {
+    // eslint-disable-next-line @typescript-eslint/no-unused-vars
+    password, apiToken, email, ...rest
+  } = user;
 
 
-  const secureUser: IUserSerializedSecurely = rest;
+  const secureUser: IUserSerializedSecurely<U> = rest;
 
 
   // omit email
   // omit email
-  if (!secureUser.isEmailPublished) {
-    delete secureUser.email;
+  if (secureUser.isEmailPublished) {
+    secureUser.email = email;
   }
   }
 
 
   return secureUser;
   return secureUser;
 };
 };
 
 
-export const serializeUserSecurely = (user?: Ref<IUser>): Ref<IUserSerializedSecurely> | undefined => {
-  // return when it is not a user object
-  if (user == null || !isPopulated(user)) {
-    return user;
-  }
+export function serializeUserSecurely<U extends IUser>(user?: U): IUserSerializedSecurely<U>;
+export function serializeUserSecurely<U extends IUser>(user?: Ref<U>): Ref<IUserSerializedSecurely<U>>;
+export function serializeUserSecurely<U extends IUser>(user?: U | Ref<U>): undefined | IUserSerializedSecurely<U> | Ref<IUserSerializedSecurely<U>> {
+  if (user == null) return user;
 
 
-  return {
-    _id: user._id,
-    ...omitInsecureAttributes(user),
-  };
-};
+  if (isRef(user) && !isPopulated(user)) return user;
+
+  return omitInsecureAttributes(user);
+}