File size: 4,097 Bytes
3bdacaf
 
 
 
 
773ca59
 
 
 
 
3bdacaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9542199
 
 
 
773ca59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bdacaf
 
 
 
 
 
 
 
 
 
 
 
 
 
9542199
3bdacaf
 
 
 
 
 
 
 
 
 
773ca59
3bdacaf
9542199
 
 
 
 
3bdacaf
 
 
 
 
 
9542199
3bdacaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const { PrismaClient } = require("@prisma/client");

const SOCIAL_REQUIREMENT_PREFIX = "User requirement from social community:";
const GENERIC_PROBLEM_STATEMENT =
  /^Build a\s+.+?\s+product for\s+.+?\s+based on the submitted idea\.$/i;
const MAX_TITLE_LENGTH = 80;

function collapseWhitespace(value) {
  return String(value || "").replace(/\s+/g, " ").trim();
}

function stripSocialRequirementPrefix(value) {
  if (!value) return "";

  const trimmed = String(value).trim();
  if (!trimmed) return "";

  if (trimmed.startsWith(SOCIAL_REQUIREMENT_PREFIX)) {
    return trimmed.slice(SOCIAL_REQUIREMENT_PREFIX.length).trim();
  }

  return trimmed;
}

function cleanProblemStatement(problemStatement, rawInputText) {
  const cleanedRawInput = stripSocialRequirementPrefix(rawInputText);
  const cleanedProblem = stripSocialRequirementPrefix(problemStatement);

  if (!cleanedProblem) {
    return cleanedRawInput;
  }

  if (GENERIC_PROBLEM_STATEMENT.test(cleanedProblem)) {
    return cleanedRawInput || cleanedProblem;
  }

  return cleanedProblem;
}

function cleanIdeaTitle(title) {
  return stripSocialRequirementPrefix(title);
}

function trimLeadIn(value) {
  return value
    .replace(/^i\s+(want|need|would\s+like|wish|hope)\s+(to\s+)?/i, "")
    .replace(/^please\s+/i, "")
    .replace(/^can\s+you\s+/i, "");
}

function truncateSummary(value, maxLength = MAX_TITLE_LENGTH) {
  const text = collapseWhitespace(value);
  if (text.length <= maxLength) {
    return text;
  }

  let clipped = text.slice(0, maxLength);
  const lastSpace = clipped.lastIndexOf(" ");

  if (lastSpace >= Math.floor(maxLength / 2)) {
    clipped = clipped.slice(0, lastSpace);
  }

  return clipped.replace(/[,:;.!?\-–—]+$/g, "").trim();
}

function deriveTitleFromInput(rawInputText, problemStatement) {
  const source =
    cleanProblemStatement(problemStatement, rawInputText) ||
    stripSocialRequirementPrefix(rawInputText);

  let text = collapseWhitespace(source);
  if (!text) {
    return "";
  }

  const contextSplit = text.split(/\bContext:\b/i)[0];
  if (contextSplit) {
    text = contextSplit;
  }

  const sentenceSplit = text.split(/[.!?]\s+/)[0];
  if (sentenceSplit) {
    text = sentenceSplit;
  }

  text = text.split("\n")[0].trim();
  text = trimLeadIn(text);
  text = collapseWhitespace(text);

  if (!text) {
    return "";
  }

  return truncateSummary(text);
}

function buildMeaningfulTitle(title, rawInputText, problemStatement) {
  const candidate = collapseWhitespace(cleanIdeaTitle(title || ""));
  const derived = deriveTitleFromInput(rawInputText, problemStatement);
  return derived || candidate || "New Product Idea";
}

async function main() {
  const databaseUrl = process.env.DATABASE_URL || "";

  if (!databaseUrl.startsWith("postgresql://") && !databaseUrl.startsWith("postgres://")) {
    console.log("Idea copy cleanup skipped. DATABASE_URL is not a PostgreSQL connection string.");
    return;
  }

  const prisma = new PrismaClient();

  try {
    const ideas = await prisma.idea.findMany({
      select: {
        id: true,
        title: true,
        rawInputText: true,
        problemStatement: true,
      },
    });

    let updates = 0;

    for (const idea of ideas) {
      const rawInputText = stripSocialRequirementPrefix(idea.rawInputText);
      const problemStatement = cleanProblemStatement(idea.problemStatement, rawInputText);
      const title = buildMeaningfulTitle(idea.title, rawInputText, problemStatement);

      if (
        title === idea.title &&
        rawInputText === idea.rawInputText &&
        problemStatement === idea.problemStatement
      ) {
        continue;
      }

      await prisma.idea.update({
        where: { id: idea.id },
        data: {
          title,
          rawInputText,
          problemStatement,
        },
      });

      updates += 1;
    }

    console.log(`Idea copy cleanup complete. Updated ${updates} row(s).`);
  } finally {
    await prisma.$disconnect();
  }
}

main().catch((error) => {
  console.error("Idea copy cleanup failed.");
  console.error(error);
  process.exit(1);
});