# Ratings

Stelace Ratings help you increasing trust on your platform. Ratings relate to User and optional Asset.

Creating a rating is as simple as:

await stelace.ratings.create({
  score: 99,
  comment: 'Awesome!',
  targetId: 'usr_WHlfQps1I3a1gJYz2I3a',
  assetId: 'ast_2l7fQps1I3a1gJYz2I3a',
  transactionId: 'trn_a3BfQps1I3a1gJYz2I3a'
})

You can optionally mention a transactionId, which can be useful for filtering in your UI.

# Aggregated ratings statistics

A single Rating probably isn’t enough to ascertain an Asset’s popularity.

Stelace provides a convenient way to aggregate Ratings in one request, so you can get the average and other stats:

await stelace.ratings.getStats({
  groupBy: 'assetId',
  assetId: 'ast_2l7fQps1I3a1gJYz2I3a'
})

You can also rank all Assets with:

await stelace.ratings.getStats({
  groupBy: 'assetId',
  computeRanking: true
})

# Advanced ratings

Let's say you want to rate users according to two criteria: User “friendliness” and Asset “cleanliness”.

# Labelled ratings

Let’s create two Ratings of different types, using label attribute:

await stelace.ratings.create({
  score: 90,
  comment: 'Cool!',
  targetId: 'usr_WHlfQps1I3a1gJYz2I3a',
  assetId: 'ast_2l7fQps1I3a1gJYz2I3a',
  transactionId: 'trn_a3BfQps1I3a1gJYz2I3a',
  label: 'friendliness'
})

await stelace.ratings.create({
  score: 30,
  comment: 'The car was dirty',
  targetId: 'usr_WHlfQps1I3a1gJYz2I3a',
  assetId: 'ast_2l7fQps1I3a1gJYz2I3a',
  transactionId: 'trn_a3BfQps1I3a1gJYz2I3a',
  label: 'cleanliness'
})

Supposing we have many more of these, we can now aggregate by label:

await stelace.ratings.getStats({
  groupBy: 'assetId',
  label: 'friendliness'
})

await stelace.ratings.getStats({
  groupBy: 'assetId',
  label: 'cleanliness'
})

# Advanced labels

You can also use wildcard in label for best flexibility.

We can regroup two previous types of Ratings as subtypes under a new “main” type, making it easier to aggregate on several label values if needed.

await stelace.ratings.create({
  score: 90,
  comment: 'Cool!',
  targetId: 'usr_WHlfQps1I3a1gJYz2I3a',
  assetId: 'ast_2l7fQps1I3a1gJYz2I3a',
  transactionId: 'trn_a3BfQps1I3a1gJYz2I3a',
  label: 'main:friendliness'
})

await stelace.ratings.create({
  score: 30,
  comment: 'The car was dirty',
  targetId: 'usr_WHlfQps1I3a1gJYz2I3a',
  assetId: 'ast_2l7fQps1I3a1gJYz2I3a',
  transactionId: 'trn_a3BfQps1I3a1gJYz2I3a',
  label: 'main:cleanliness'
})

We can now get an average including both “friendliness” and “cleanliness” Ratings:

await stelace.ratings.getStats({
  groupBy: 'assetId',
  label: 'main:*'
})