Skip to content

Instantly share code, notes, and snippets.

@boxpositron
Last active October 28, 2022 12:47
Show Gist options
  • Save boxpositron/e3b2b328b3b48076631bf4074f2f81da to your computer and use it in GitHub Desktop.
Save boxpositron/e3b2b328b3b48076631bf4074f2f81da to your computer and use it in GitHub Desktop.
Tracksend Segments

Segment Model

Proposed segment model design

Requirements

Structure

Proposed mongoose Schema and associated interfaces.

interface Segment {
    userId: number;
    name: string;
    groupId: string;
    contactSize: number;
    deleted: boolean;
    filters: {
        field: string;
        operator: string;
        value: string;
    }[]
}

const schema = mongoose.Schema<Segment>({
  userId: {
    type: Number,
    index: true,
  },
  name: {
    type: String,
  },
  groupId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "groups",
  },
  contactSize: Number,
  deleted: {
    type: Boolean,
    default: false,
    select: false,
  },
  // Group segment filter
  filters: [
    {
      field: {
        type: String,
        enum: Object.values(CONTACT_FIELDS),
      },
      operator: {
        type: String,
        enum: Object.values(GROUP_SEGMENT_OPERATORS),
      },
      value: String,
    },
  ],
  // Prebuilt segment filter
  filters: [
    {
      condition: {
        type: String,
        enum: Object.values(SEGMENT_PROPERTIES),
      },
      range: Array,
    },
  ],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment